import { NextResponse } from 'next/server';
import { requireWorkspacePrincipal } from "@/lib/auth/workspace-auth";
import { query, queryOne } from '@/lib/db-postgres';

export async function GET(request: Request) {
  try {
    // 1. Authenticate the Workspace API key or Browser Session
    const principal = await requireWorkspacePrincipal(request);
    if (principal instanceof NextResponse) return principal;

    // 2. Fetch user email from principal.userId
    const userRow = await queryOne<{ data: any }>(
      `SELECT data FROM fs_users WHERE id = $1 LIMIT 1`,
      [principal.userId]
    );
    const email = userRow?.data?.email;
    if (!email) {
      return NextResponse.json({ error: 'User email not found' }, { status: 404 });
    }

    // Fetch projects joined on user email
    const projects = await query<any>(`
      SELECT p.id, p.data, p.workspace, p.slug
      FROM fs_projects p
      JOIN fs_users u ON u.id = p.user_id
      WHERE u.data->>'email' = $1
      ORDER BY (p.data->>'updatedAt') DESC NULLS LAST
    `, [email]);

    // Fetch session stats per project
    const sessionStats = await query<any>(`
      SELECT
        s.data->>'projectId' AS project_id,
        COUNT(*)::int AS session_count,
        COALESCE(SUM((s.data->>'cost')::float), 0) AS total_cost
      FROM fs_sessions s
      JOIN fs_users u ON u.id = s.user_id
      WHERE u.data->>'email' = $1
      GROUP BY s.data->>'projectId'
    `, [email]);

    const statsByProject = new Map(sessionStats.map((s: any) => [s.project_id, s]));

    const result = projects.map((p: any) => {
      const stats = statsByProject.get(p.id) || { session_count: 0, total_cost: 0 };
      return {
        id: p.id,
        ...p.data,
        stats: {
          sessions: stats.session_count || 0,
          costs: parseFloat(stats.total_cost) || 0,
        },
      };
    });

    return NextResponse.json({ projects: result });
  } catch (error) {
    console.error('[GET /api/projects] Error:', error);
    return NextResponse.json(
      { error: 'Failed to fetch projects', details: error instanceof Error ? error.message : String(error) },
      { status: 500 }
    );
  }
}
