import { NextResponse } from 'next/server';
import { adminDb } from '@/lib/firebase/admin';
import { FieldValue, Timestamp } from 'firebase-admin/firestore';

export async function POST(request: Request) {
  try {
    const body = await request.json();
    const { apiKey, sessionData } = body;

    if (!apiKey) {
      return NextResponse.json(
        { error: 'API key is required' },
        { status: 401 }
      );
    }

    // Verify API key and get userId
    const keyDoc = await adminDb.collection('apiKeys').doc(apiKey).get();
    
    if (!keyDoc.exists || !keyDoc.data()?.isActive) {
      return NextResponse.json(
        { error: 'Invalid or inactive API key' },
        { status: 401 }
      );
    }

    const userId = keyDoc.data()!.userId;
    
    if (!userId) {
      return NextResponse.json(
        { error: 'User not found for API key' },
        { status: 401 }
      );
    }

    // Update last used timestamp
    await keyDoc.ref.update({
      lastUsed: FieldValue.serverTimestamp(),
    });

    // Check if workspace has an associated project
    let projectId = sessionData.projectId || null;
    let needsProjectAssociation = false;
    
    if (!projectId && sessionData.workspacePath) {
      // Try to find a project with this workspace path
      const projectsSnapshot = await adminDb
        .collection('projects')
        .where('userId', '==', userId)
        .where('workspacePath', '==', sessionData.workspacePath)
        .limit(1)
        .get();
      
      if (!projectsSnapshot.empty) {
        // Found a matching project, auto-associate
        projectId = projectsSnapshot.docs[0].id;
        console.log(`✅ Auto-associated session with project: ${projectId}`);
      } else {
        // No matching project found, flag for user action
        needsProjectAssociation = true;
        console.log(`⚠️ New workspace detected: ${sessionData.workspacePath}`);
      }
    }

    // Create session document
    const sessionRef = adminDb.collection('sessions').doc();
    await sessionRef.set({
      id: sessionRef.id,
      userId,
      projectId,
      
      // Session data
      startTime: Timestamp.fromMillis(new Date(sessionData.startTime).getTime()),
      endTime: sessionData.endTime ? Timestamp.fromMillis(new Date(sessionData.endTime).getTime()) : null,
      duration: sessionData.duration || null,
      
      // Project context
      workspacePath: sessionData.workspacePath || null,
      workspaceName: sessionData.workspacePath ? sessionData.workspacePath.split('/').pop() : null,
      needsProjectAssociation,
      
      // AI usage
      model: sessionData.model || 'unknown',
      tokensUsed: sessionData.tokensUsed || 0,
      cost: sessionData.cost || 0,
      
      // Context
      filesModified: sessionData.filesModified || [],
      conversationSummary: sessionData.conversationSummary || null,
      conversation: sessionData.conversation || [],
      messageCount: sessionData.conversation?.length || 0,
      
      createdAt: FieldValue.serverTimestamp(),
    });

    return NextResponse.json({
      success: true,
      sessionId: sessionRef.id,
      message: 'Session tracked successfully',
    });
  } catch (error) {
    console.error('Error tracking session:', error);
    return NextResponse.json(
      {
        error: 'Failed to track session',
        details: error instanceof Error ? error.message : String(error),
      },
      { status: 500 }
    );
  }
}

