import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth/authOptions';
import { query } from '@/lib/db-postgres';

export async function POST(request: Request) {
  try {
    const session = await getServerSession(authOptions);
    if (!session?.user?.email) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }

    const { projectId } = await request.json();
    if (!projectId) {
      return NextResponse.json({ error: 'Project ID is required' }, { status: 400 });
    }

    // Verify ownership
    const rows = await query<{ id: string; data: any }>(`
      SELECT p.id, p.data
      FROM fs_projects p
      JOIN fs_users u ON u.id = p.user_id
      WHERE p.id = $1 AND u.data->>'email' = $2
      LIMIT 1
    `, [projectId, session.user.email]);

    if (rows.length === 0) {
      return NextResponse.json({ error: 'Project not found or unauthorized' }, { status: 404 });
    }

    // Unlink sessions
    const sessionResult = await query(`
      SELECT COUNT(*)::int AS count FROM fs_sessions WHERE data->>'projectId' = $1
    `, [projectId]);
    const sessionCount = sessionResult[0]?.count || 0;

    await query(`
      UPDATE fs_sessions
      SET data = jsonb_set(
        jsonb_set(data, '{projectId}', 'null'),
        '{needsProjectAssociation}', 'true'
      )
      WHERE data->>'projectId' = $1
    `, [projectId]);

    // Delete the project
    await query(`DELETE FROM fs_projects WHERE id = $1`, [projectId]);

    return NextResponse.json({ success: true, message: 'Project deleted successfully', sessionsPreserved: sessionCount });
  } catch (error) {
    console.error('[POST /api/projects/delete] Error:', error);
    return NextResponse.json(
      { error: 'Failed to delete project', details: error instanceof Error ? error.message : String(error) },
      { status: 500 }
    );
  }
}
