import { NextResponse } from 'next/server';
import { authSession } from "@/lib/auth/session-server";
import { query } from '@/lib/db-postgres';

/**
 * GET — returns surfaces[] and surfaceThemes{} for the project.
 */
export async function GET(
  _req: Request,
  { params }: { params: Promise<{ projectId: string }> }
) {
  try {
    const { projectId } = await params;
    const session = await authSession();
    if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

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

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

    const data = rows[0].data ?? {};
    return NextResponse.json({
      surfaces: (data.surfaces ?? []) as string[],
      surfaceThemes: (data.surfaceThemes ?? {}) as Record<string, string>,
    });
  } catch (err) {
    console.error('[design-surfaces GET]', err);
    return NextResponse.json({ error: 'Internal error' }, { status: 500 });
  }
}

/**
 * PATCH — two operations:
 *   { surfaces: string[] }              — save the active surface list
 *   { surface: string, theme: string }  — lock in a theme for one surface
 */
export async function PATCH(
  req: Request,
  { params }: { params: Promise<{ projectId: string }> }
) {
  try {
    const { projectId } = await params;
    const session = await authSession();
    if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

    // Step 1: read current data — explicit ::text casts on every param
    let rows: { data: Record<string, unknown> }[];
    try {
      rows = await query<{ data: Record<string, unknown> }>(
        `SELECT p.data FROM fs_projects p
         JOIN fs_users u ON u.id = p.user_id
         WHERE p.id = $1::text AND u.data->>'email' = $2::text
         LIMIT 1`,
        [projectId, session.user.email]
      );
    } catch (selErr) {
      console.error('[design-surfaces PATCH] SELECT failed:', selErr);
      return NextResponse.json({ error: 'Internal error (select)' }, { status: 500 });
    }

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

    const current = rows[0].data ?? {};
    const body = await req.json() as
      | { surfaces: string[] }
      | { surface: string; theme: string };

    let updated: Record<string, unknown>;

    if ('surfaces' in body) {
      updated = { ...current, surfaces: body.surfaces, updatedAt: new Date().toISOString() };
    } else if ('surface' in body && 'theme' in body) {
      const existing = (current.surfaceThemes ?? {}) as Record<string, string>;
      updated = {
        ...current,
        surfaceThemes: { ...existing, [body.surface]: body.theme },
        updatedAt: new Date().toISOString(),
      };
    } else {
      return NextResponse.json({ error: 'Invalid body' }, { status: 400 });
    }

    // Step 2: write back — explicit ::text cast on id param, ::jsonb on data param
    try {
      await query(
        `UPDATE fs_projects SET data = $1::jsonb WHERE id = $2::text`,
        [JSON.stringify(updated), projectId]
      );
    } catch (updErr) {
      console.error('[design-surfaces PATCH] UPDATE failed:', updErr);
      return NextResponse.json({ error: 'Internal error (update)' }, { status: 500 });
    }

    return NextResponse.json({ success: true });
  } catch (err) {
    console.error('[design-surfaces PATCH]', err);
    return NextResponse.json({ error: 'Internal error' }, { status: 500 });
  }
}
