import { NextResponse } from "next/server";
import { serializeOperationMetadata } from "@open-mercato/shared/lib/commands/operationMetadata";
import { runWithCacheTenant } from "@open-mercato/cache";
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { InboxProposal, InboxProposalAction } from "../../../../../../data/entities.js";
import { executeAction } from "../../../../../../lib/executionEngine.js";
import { resolveCache, invalidateCountsCache } from "../../../../../../lib/cache.js";
import {
  resolveRequestContext,
  resolveActionAndProposal,
  resolveCrossModuleEntities,
  toExecutionContext,
  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 resolved = await resolveActionAndProposal(new URL(req.url), ctx);
    if (isErrorResponse(resolved)) return resolved;
    const entities = resolveCrossModuleEntities(ctx.container);
    const result = await executeAction(resolved.action, toExecutionContext(ctx, entities));
    if (!result.success) {
      return NextResponse.json(
        { error: result.error || "Failed to execute action" },
        { status: result.statusCode || 400 }
      );
    }
    const freshAction = await findOneWithDecryption(
      ctx.em,
      InboxProposalAction,
      { id: resolved.action.id, deletedAt: null },
      void 0,
      ctx.scope
    );
    const freshProposal = await findOneWithDecryption(
      ctx.em,
      InboxProposal,
      { id: resolved.proposal.id, deletedAt: null },
      void 0,
      ctx.scope
    );
    const cache = resolveCache(ctx.container);
    await runWithCacheTenant(ctx.tenantId, () => invalidateCountsCache(cache, ctx.tenantId));
    const response = NextResponse.json({
      ok: true,
      action: freshAction ? {
        id: freshAction.id,
        status: freshAction.status,
        createdEntityId: freshAction.createdEntityId,
        createdEntityType: freshAction.createdEntityType,
        executedAt: freshAction.executedAt,
        executedByUserId: freshAction.executedByUserId
      } : null,
      proposal: freshProposal ? {
        id: freshProposal.id,
        status: freshProposal.status
      } : null
    });
    const operationLogEntry = result.operationLogEntry;
    if (operationLogEntry?.undoToken && operationLogEntry.id && operationLogEntry.commandId) {
      const executedAt = operationLogEntry.createdAt instanceof Date ? operationLogEntry.createdAt.toISOString() : typeof operationLogEntry.createdAt === "string" && operationLogEntry.createdAt.trim().length > 0 ? operationLogEntry.createdAt : (/* @__PURE__ */ new Date()).toISOString();
      response.headers.set(
        "x-om-operation",
        serializeOperationMetadata({
          id: operationLogEntry.id,
          undoToken: operationLogEntry.undoToken,
          commandId: operationLogEntry.commandId,
          actionLabel: operationLogEntry.actionLabel ?? null,
          resourceKind: operationLogEntry.resourceKind ?? null,
          resourceId: operationLogEntry.resourceId ?? null,
          executedAt
        })
      );
    }
    return response;
  } catch (err) {
    return handleRouteError(err, "execute action");
  }
}
const openApi = {
  tag: "InboxOps",
  summary: "Accept action",
  methods: {
    POST: {
      summary: "Accept and execute a proposal action",
      description: "Executes the action and creates the entity in the target module. Returns 409 if already processed.",
      responses: [
        { status: 200, description: "Action executed successfully" },
        { status: 403, description: "Insufficient permissions in target module" },
        { status: 404, description: "Action not found" },
        { status: 409, description: "Action already processed" }
      ]
    }
  }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
