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

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

export interface PreviewApp {
  name: string;
  url: string | null;
  status: string;
  coolifyUuid: string | null;
  gitRepo: string | null;
}

export async function GET(
  _req: Request,
  { params }: { params: Promise<{ projectId: string }> }
) {
  const { projectId } = await params;

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

  // 1. Load project — get the Gitea repo name
  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/scout-ai-engine"

  if (!giteaRepo) {
    return NextResponse.json({ apps: [], message: 'No Gitea repo linked to this project' });
  }

  // 2. Build the possible Gitea remote URLs for this repo (with and without .git)
  const repoBase = `${GITEA_BASE}/${giteaRepo}`;
  const repoUrls = new Set([repoBase, `${repoBase}.git`]);

  // 3. Fetch all Coolify applications and match by git_repository
  let coolifyApps: CoolifyApplication[] = [];
  try {
    coolifyApps = await listApplications();
  } catch (err) {
    console.error('[preview-url] Coolify fetch failed:', err);
    // Fall back to stored data
  }

  const matched = coolifyApps.filter(app =>
    app.git_repository && repoUrls.has(app.git_repository.replace(/\/$/, ''))
  );

  if (matched.length > 0) {
    const apps: PreviewApp[] = matched.map(app => ({
      name: app.name,
      url: app.fqdn
        ? (app.fqdn.startsWith('http') ? app.fqdn : `https://${app.fqdn}`)
        : null,
      status: app.status ?? 'unknown',
      coolifyUuid: app.uuid,
      gitRepo: app.git_repository ?? null,
    }));
    return NextResponse.json({ apps, source: 'coolify' });
  }

  // 4. Fallback: use whatever URL was stored by the Coolify webhook
  const lastDeployment = (data as any).contextSnapshot?.lastDeployment ?? null;
  if (lastDeployment?.url) {
    const apps: PreviewApp[] = [{
      name: giteaRepo.split('/').pop() ?? 'app',
      url: lastDeployment.url.startsWith('http') ? lastDeployment.url : `https://${lastDeployment.url}`,
      status: lastDeployment.status === 'finished' ? 'running' : lastDeployment.status ?? 'unknown',
      coolifyUuid: lastDeployment.applicationUuid ?? null,
      gitRepo: giteaRepo,
    }];
    return NextResponse.json({ apps, source: 'webhook' });
  }

  return NextResponse.json({
    apps: [],
    message: `No Coolify app found for repo: ${giteaRepo}`,
    giteaRepo,
  });
}
