import { NextResponse } from "next/server";
import { findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { InboxProposalAction } from "../../../../data/entities.js";
import { translateProposalSchema } from "../../../../data/validators.js";
import { translateProposalContent } from "../../../../lib/translationProvider.js";
import {
  resolveRequestContext,
  resolveProposal,
  handleRouteError,
  isErrorResponse
} from "../../../routeHelpers.js";
const metadata = {
  POST: { requireAuth: true, requireFeatures: ["inbox_ops.proposals.manage"] }
};
async function POST(req) {
  try {
    const ctx = await resolveRequestContext(req);
    const proposal = await resolveProposal(new URL(req.url), ctx);
    if (isErrorResponse(proposal)) return proposal;
    let body;
    try {
      body = await req.json();
    } catch {
      return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
    }
    const parsed = translateProposalSchema.safeParse(body);
    if (!parsed.success) {
      return NextResponse.json({ error: "Invalid request", details: parsed.error.issues }, { status: 400 });
    }
    const { targetLocale } = parsed.data;
    const proposalLanguage = proposal.workingLanguage || "en";
    if (proposalLanguage === targetLocale) {
      return NextResponse.json({ error: "Proposal is already in the requested language" }, { status: 400 });
    }
    const cached = proposal.translations?.[targetLocale];
    if (cached) {
      return NextResponse.json({ translation: cached, cached: true });
    }
    const actions = await findWithDecryption(
      ctx.em,
      InboxProposalAction,
      { proposalId: proposal.id, organizationId: ctx.organizationId, tenantId: ctx.tenantId, deletedAt: null },
      { orderBy: { sortOrder: "ASC" } },
      ctx.scope
    );
    const actionDescriptions = {};
    for (const action of actions) {
      actionDescriptions[action.id] = action.description;
    }
    const result = await translateProposalContent({
      summary: proposal.summary,
      actionDescriptions,
      sourceLanguage: proposalLanguage,
      targetLocale
    });
    const entry = {
      summary: result.summary,
      actions: result.actions,
      translatedAt: (/* @__PURE__ */ new Date()).toISOString()
    };
    const translations = proposal.translations || {};
    translations[targetLocale] = entry;
    proposal.translations = translations;
    await ctx.em.flush();
    return NextResponse.json({ translation: entry, cached: false });
  } catch (err) {
    return handleRouteError(err, "translate proposal");
  }
}
const openApi = {
  tag: "InboxOps",
  summary: "Translate proposal",
  methods: {
    POST: {
      summary: "Translate proposal content",
      description: "Translates the proposal summary and action descriptions to the target locale. Results are cached.",
      responses: [
        { status: 200, description: "Translation result" },
        { status: 400, description: "Invalid target locale or same language" },
        { status: 404, description: "Proposal not found" }
      ]
    }
  }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
