import { NextRequest, NextResponse } from 'next/server';
import { adminDb } from '@/lib/firebase/admin';

export async function GET(request: NextRequest) {
  try {
    const projectId = request.nextUrl.searchParams.get('projectId');
    
    if (!projectId) {
      return NextResponse.json(
        { error: 'Missing projectId parameter' },
        { status: 400 }
      );
    }

    // Check if project exists
    const projectDoc = await adminDb.collection('projects').doc(projectId).get();
    
    if (!projectDoc.exists) {
      // List all projects to help debug
      const allProjectsSnapshot = await adminDb.collection('projects').limit(20).get();
      const allProjects = allProjectsSnapshot.docs.map(doc => ({
        id: doc.id,
        name: doc.data().name,
        userId: doc.data().userId,
        createdAt: doc.data().createdAt
      }));

      return NextResponse.json({
        exists: false,
        projectId,
        message: 'Project not found',
        availableProjects: allProjects
      });
    }

    const projectData = projectDoc.data();
    
    return NextResponse.json({
      exists: true,
      projectId,
      project: {
        name: projectData?.name,
        userId: projectData?.userId,
        createdAt: projectData?.createdAt,
        githubRepo: projectData?.githubRepo
      }
    });
  } catch (error) {
    console.error('Error checking project:', error);
    return NextResponse.json(
      { error: 'Failed to check project', details: error instanceof Error ? error.message : String(error) },
      { status: 500 }
    );
  }
}







