import { NextResponse } from "next/server";
import { runWithCacheTenant } from "@open-mercato/cache";
import {
  runCrudMutationGuardAfterSuccess,
  validateCrudMutationGuard
} from "@open-mercato/shared/lib/crud/mutation-guard";
import { categorizeProposalSchema } from "../../../../data/validators.js";
import { resolveCache, invalidateCountsCache } from "../../../../lib/cache.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;
    const body = await req.json();
    const parsed = categorizeProposalSchema.safeParse(body);
    if (!parsed.success) {
      const issues = parsed.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ");
      return NextResponse.json({ error: `Invalid category: ${issues}` }, { status: 400 });
    }
    const guardResult = await validateCrudMutationGuard(ctx.container, {
      tenantId: ctx.tenantId,
      organizationId: ctx.organizationId,
      userId: ctx.userId,
      resourceKind: "inbox_ops:inbox_proposal",
      resourceId: proposal.id,
      operation: "update",
      requestMethod: req.method,
      requestHeaders: req.headers,
      mutationPayload: parsed.data
    });
    if (guardResult && !guardResult.ok) {
      return NextResponse.json(guardResult.body, { status: guardResult.status });
    }
    const previousCategory = proposal.category || null;
    proposal.category = parsed.data.category;
    await ctx.em.flush();
    if (guardResult?.ok && guardResult.shouldRunAfterSuccess) {
      await runCrudMutationGuardAfterSuccess(ctx.container, {
        tenantId: ctx.tenantId,
        organizationId: ctx.organizationId,
        userId: ctx.userId,
        resourceKind: "inbox_ops:inbox_proposal",
        resourceId: proposal.id,
        operation: "update",
        requestMethod: req.method,
        requestHeaders: req.headers,
        metadata: guardResult.metadata ?? null
      });
    }
    const cache = resolveCache(ctx.container);
    await runWithCacheTenant(ctx.tenantId, () => invalidateCountsCache(cache, ctx.tenantId));
    return NextResponse.json({
      ok: true,
      category: parsed.data.category,
      previousCategory
    });
  } catch (err) {
    return handleRouteError(err, "categorize proposal");
  }
}
const openApi = {
  tag: "InboxOps",
  summary: "Categorize proposal",
  methods: {
    POST: {
      summary: "Set or change the category of a proposal",
      description: "Assigns a category to a proposal. Returns the new and previous category for undo support.",
      responses: [
        { status: 200, description: "Category updated" },
        { status: 400, description: "Invalid category value" },
        { status: 404, description: "Proposal not found" }
      ]
    }
  }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
