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

const GITEA_API_URL = process.env.GITEA_API_URL ?? 'https://git.vibnai.com';
const GITEA_API_TOKEN = process.env.GITEA_API_TOKEN ?? '';

async function giteaGet(path: string) {
  const res = await fetch(`${GITEA_API_URL}/api/v1${path}`, {
    headers: { Authorization: `token ${GITEA_API_TOKEN}` },
    next: { revalidate: 30 },
  });
  if (!res.ok) throw new Error(`Gitea ${res.status}: ${path}`);
  return res.json();
}

/**
 * GET — returns the project's apps/ directories from Gitea + saved designPackages.
 * Response: { apps: [{ name, path, type }], designPackages: { appName: packageId } }
 */
export async function GET(
  _req: Request,
  { params }: { params: Promise<{ projectId: string }> }
) {
  const { projectId } = await params;

  const session = await getServerSession(authOptions);
  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 AND u.data->>'email' = $2 LIMIT 1`,
    [projectId, session.user.email]
  );

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

  const data = rows[0].data ?? {};
  const giteaRepo = data.giteaRepo as string | undefined; // e.g. "mark/sportsy"
  const designPackages = (data.designPackages ?? {}) as Record<string, string>;

  let apps: { name: string; path: string }[] = [];

  if (giteaRepo) {
    try {
      const contents: Array<{ name: string; path: string; type: string }> =
        await giteaGet(`/repos/${giteaRepo}/contents/apps`);
      apps = contents
        .filter((item) => item.type === 'dir')
        .map(({ name, path }) => ({ name, path }));
    } catch {
      // Repo may not have an apps/ dir yet — return empty list gracefully
    }
  }

  return NextResponse.json({ apps, designPackages, giteaRepo });
}

/**
 * PATCH — saves { appName, packageId } → stored in fs_projects.data.designPackages
 */
export async function PATCH(
  req: Request,
  { params }: { params: Promise<{ projectId: string }> }
) {
  const { projectId } = await params;

  const session = await getServerSession(authOptions);
  if (!session?.user?.email) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }

  const { appName, packageId } = await req.json() as { appName: string; packageId: string };

  if (!appName || !packageId) {
    return NextResponse.json({ error: 'appName and packageId are required' }, { status: 400 });
  }

  await query(
    `UPDATE fs_projects p
     SET data = data || jsonb_build_object(
       'designPackages',
       COALESCE(data->'designPackages', '{}'::jsonb) || jsonb_build_object($3, $4)
     ),
     updated_at = NOW()
     FROM fs_users u
     WHERE p.id = $1 AND p.user_id = u.id AND u.data->>'email' = $2`,
    [projectId, session.user.email, appName, packageId]
  );

  return NextResponse.json({ success: true });
}
