import { NextResponse } from 'next/server';
import { getAdminDb } from '@/lib/firebase/admin';

export async function GET(request: Request) {
  try {
    const url = new URL(request.url);
    const projectId = url.searchParams.get('projectId');

    if (!projectId) {
      return NextResponse.json({ error: 'Missing projectId' }, { status: 400 });
    }

    const adminDb = getAdminDb();
    
    // Get contextSources subcollection
    const contextSourcesRef = adminDb
      .collection('projects')
      .doc(projectId)
      .collection('contextSources');
    
    const snapshot = await contextSourcesRef.get();
    
    const sources = snapshot.docs.map(doc => ({
      id: doc.id,
      ...doc.data(),
    }));

    return NextResponse.json({
      projectId,
      count: sources.length,
      sources,
    });
  } catch (error) {
    console.error('[debug/context-sources] Error:', error);
    return NextResponse.json(
      {
        error: 'Failed to fetch context sources',
        details: error instanceof Error ? error.message : String(error),
      },
      { status: 500 },
    );
  }
}

