import type { FastifyInstance } from "fastify";
import { z } from "zod";
import { id, invalid, parse, readUpload, route, sendFile } from "../../core/http/route.js";
import * as s from "./outcomes.service.js";
import { outcomeExport } from "./export.service.js";

const line = z.object({ traineeId: id, value: z.enum(["yes", "no"]).optional(), eventDate: z.string().optional(), reason: z.string().max(60).optional(), details: z.record(z.string().max(500)).optional() });
const O = "/api/projects/:projectId/outcome-batches", R = "/api/projects/:projectId/outcome-resubmission-requests";
export async function outcomeRoutes(app: FastifyInstance): Promise<void> {
  const p = "/projects/:p", k = `${p}/classes/:id/outcomes/:type`;
  route(app, "GET", `${p}/outcomes/board`, { perm: "outcome.view" }, async (c) => s.board(c));
  route(app, "GET", `${p}/outcomes/templates`, { perm: "outcome.view", v1: ["GET /api/projects/:projectId/outcome-templates"] }, async () => s.templates());
  route(app, "GET", `${p}/outcomes/status`, { perm: "outcome.view", v1: ["GET /api/projects/:projectId/trainee-outcomes"] }, async (c) => s.statusList(c));
  route(app, "GET", `${p}/outcomes/export/:type`, { perm: "report.export", v1: ["GET /api/projects/:projectId/reports/outcomes/:reportType.csv"] }, async (c) => { const f = await outcomeExport(c, c.params["type"]!, c.query["providerId"], c.query["format"] ?? "csv"); return sendFile(c, f.name, f.xlsx ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "text/csv; charset=utf-8", f.body); });
  route(app, "GET", `${p}/outcomes/dropout-monitoring`, { perm: "outcome.view", v1: ["GET /api/projects/:projectId/dropout-projections"] }, async (c) => s.dropoutMonitoring(c));
  route(app, "GET", `${k}/context`, { perm: "outcome.view" }, async (c) => s.context(c, c.params["id"]!, c.params["type"]!, c.query["stage"]));
  route(app, "GET", `${k}/template`, { perm: "outcome.view", v1: ["GET /api/projects/:projectId/outcome-templates/:reportType/csv"] }, async (c) => { const f = await s.template(c, c.params["id"]!, c.params["type"]!, c.query["stage"], c.query["format"] ?? "xlsx"); return sendFile(c, f.name, f.xlsx ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "text/csv; charset=utf-8", f.body); });
  route(app, "POST", `${k}/parse`, { perm: "outcome.submit", v1: [`POST ${O}/import-csv`] }, async (c) => { const u = await readUpload(c), f = u.files[0] ?? invalid("file", "Choose a file."); return s.parseFile(c, c.params["id"]!, c.params["type"]!, u.fields["stage"], f.name, f.data); });
  route(app, "POST", `${k}/submit`, { perm: "outcome.submit", v1: [`POST ${O}`, `POST ${R}`] }, async (c) => s.submit(c, c.params["id"]!, c.params["type"]!, parse(z.object({ lines: z.array(line).max(2000), reasonForChange: z.string().max(1000).optional(), filename: z.string().max(190).optional() }), c.body)));
  const l = `${p}/sites/:id/outcomes/:type`;
  route(app, "GET", `${l}/context`, { perm: "outcome.submit" }, async (c) => s.siteContext(c, c.params["id"]!, c.params["type"]!));
  route(app, "GET", `${l}/template`, { perm: "outcome.submit" }, async (c) => { const f = await s.siteTemplate(c, c.params["id"]!, c.params["type"]!, c.query["format"] ?? "xlsx"); return sendFile(c, f.name, f.xlsx ? "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : "text/csv; charset=utf-8", f.body); });
  route(app, "POST", `${l}/parse`, { perm: "outcome.submit" }, async (c) => { const f = (await readUpload(c)).files[0] ?? invalid("file", "Choose a file."); return s.siteParse(c, c.params["id"]!, c.params["type"]!, f.name, f.data); });
  route(app, "POST", `${l}/submit`, { perm: "outcome.submit" }, async (c) => s.siteSubmit(c, c.params["id"]!, c.params["type"]!, parse(z.object({ classes: z.array(z.object({ id, lines: z.array(line).max(2000) })).min(1).max(100), filename: z.string().max(190).optional() }), c.body)));
  route(app, "GET", `${p}/outcome-reports`, { perm: "outcome.view", v1: [`GET ${O}`, `GET ${R}`] }, async (c) => s.listReports(c));
  route(app, "GET", `${p}/outcome-reports/:id`, { perm: "outcome.view" }, async (c) => s.getReport(c, c.params["id"]!));
  route(app, "POST", `${p}/outcome-reports/:id/return`, { perm: "outcome.review" }, async (c) => s.returnReport(c, c.params["id"]!, parse(z.object({ reason: z.string().trim().min(5, "Explain what must be corrected.").max(480) }), c.body).reason));
  route(app, "POST", `${p}/outcome-reports/:id/discard`, { perm: "project.manage", v1: [`DELETE ${O}/:batchId`] }, async (c) => s.discardReport(c, c.params["id"]!, parse(z.object({ reason: z.string().trim().min(5).max(480) }), c.body).reason));
  route(app, "POST", `${p}/outcome-reports/:id/approve-resubmission`, { perm: "outcome.resubmit.approve", v1: [`POST ${R}/:requestId/decision`] }, async (c) => { const b = parse(z.object({ decision: z.enum(["approve", "reject"]), note: z.string().max(480).default("") }), c.body); return s.decideResubmission(c, c.params["id"]!, b.decision, b.note); });
}
