import { z } from "zod";
import { withV3ApiWrapper } from "@/app/api/v3/lib/api-wrapper";
import { ConcurrencyLimiter } from "@/app/lib/api/concurrency-limiter";
import { ZExportRequest } from "@/lib/analysis-export/export-request";
import { EXPORT_MAX_CONCURRENT, EXPORT_MAX_CONCURRENT_PER_USER } from "./lib/export-limits";
import { handleExportRequest } from "./lib/handle-export";

// @formbricks/database and the archive writers are Node-only.
export const runtime = "nodejs";
export const dynamic = "force-dynamic";

/**
 * `POST /api/surveys/{surveyId}/export` — the Export modal's download (ENG-6792).
 *
 * Internal and session-only, like the attachments ZIP route it is modelled on: no OpenAPI entry, no
 * compatibility promise, the only caller is this app's export manager. It is a POST because a selection
 * of rows travels as a list of ids, which a query string cannot carry. `?dryRun=true` returns the counts
 * for the modal's "Include X responses" line instead of a file.
 *
 * The export runs in the request. The CSV and the zip stream; Excel is built whole, one build per process. A client that goes
 * away aborts `req.signal`, which stops the reading, so leaving the page cancels the export.
 */

const paramsSchema = z.object({ surveyId: z.cuid2() });

const querySchema = z.object({
  dryRun: z
    .enum(["true", "false"])
    .optional()
    .transform((value) => value === "true"),
});

/**
 * Per process, and per signed-in user: an export holds a database connection and a stream for minutes.
 * The modal's `dryRun` count requests are not exports and take no slot: it fires one per option the owner
 * toggles, and counting them here turned a few quick clicks into a 429 or 503 with no count shown.
 */
const exportConcurrency = new ConcurrencyLimiter(EXPORT_MAX_CONCURRENT, {
  maxPerKey: EXPORT_MAX_CONCURRENT_PER_USER,
});

export const POST = withV3ApiWrapper({
  auth: "session",
  action: "exported",
  targetType: "file",
  concurrency: {
    limiter: exportConcurrency,
    retryAfterSeconds: 15,
    appliesTo: (req) => req.nextUrl.searchParams.get("dryRun") !== "true",
  },
  schemas: { params: paramsSchema, query: querySchema, body: ZExportRequest },
  handler: ({ req, parsedInput, authentication, requestId, instance, auditLog }) =>
    handleExportRequest({
      surveyId: parsedInput.params.surveyId,
      body: parsedInput.body,
      dryRun: parsedInput.query.dryRun,
      authentication,
      auditLog,
      signal: req.signal,
      requestId,
      instance,
    }),
});
