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

// TEMPORARY: For debugging/testing only - no auth required
export async function GET(request: NextRequest) {
  try {
    const projectId = request.nextUrl.searchParams.get('projectId');

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

    // Delete all cursor conversations for this project
    const conversationsSnapshot = await adminDb
      .collection('projects')
      .doc(projectId)
      .collection('cursorConversations')
      .get();

    const batch = adminDb.batch();
    
    conversationsSnapshot.docs.forEach((doc: FirebaseFirestore.QueryDocumentSnapshot) => {
      batch.delete(doc.ref);
    });

    // Also delete the messages data document
    const messagesRef = adminDb
      .collection('projects')
      .doc(projectId)
      .collection('cursorData')
      .doc('messages');
    
    batch.delete(messagesRef);

    await batch.commit();

    return NextResponse.json({
      success: true,
      deletedCount: conversationsSnapshot.size,
      message: 'All cursor conversations cleared'
    });

  } catch (error) {
    console.error('Error clearing cursor conversations:', error);
    return NextResponse.json(
      { error: 'Failed to clear conversations', details: error instanceof Error ? error.message : String(error) },
      { status: 500 }
    );
  }
}

