/**
 * POST /api/workspaces/[slug]/apps/[uuid]/deploy
 *
 * Trigger a deploy on a Coolify app. Guard: app must belong to this
 * workspace's Coolify project before we forward the call.
 */

import { NextResponse } from "next/server";
import { requireWorkspacePrincipal } from "@/lib/auth/workspace-auth";
import {
  deployApplication,
  getApplicationInProject,
  getServiceInProject,
  TenantError,
} from "@/lib/coolify";

export async function POST(
  request: Request,
  { params }: { params: Promise<{ slug: string; uuid: string }> },
) {
  const { slug, uuid } = await params;
  const principal = await requireWorkspacePrincipal(request, {
    targetSlug: slug,
  });
  if (principal instanceof NextResponse) return principal;

  const ws = principal.workspace;
  if (!ws.coolify_project_uuid) {
    return NextResponse.json(
      { error: "Workspace has no Coolify project yet" },
      { status: 503 },
    );
  }

  try {
    // Tenant check before any mutation.
    // In Coolify v4, "deploying" a UUID can apply to an Application or a Service.
    // Try application first; if it throws a 404/not found, try checking if it's a service.
    try {
      await getApplicationInProject(uuid, ws.coolify_project_uuid);
    } catch (e: any) {
      if (e?.message?.includes("404") || e?.status === 404) {
        await getServiceInProject(uuid, ws.coolify_project_uuid);
      } else {
        throw e;
      }
    }

    const result = await deployApplication(uuid);
    return NextResponse.json({
      ok: true,
      deploymentUuid: result.deployment_uuid,
      appUuid: uuid,
    });
  } catch (err) {
    if (err instanceof TenantError) {
      return NextResponse.json({ error: err.message }, { status: 403 });
    }
    return NextResponse.json(
      {
        error: "Deploy failed",
        details: err instanceof Error ? err.message : String(err),
      },
      { status: 502 },
    );
  }
}
