import { NextResponse } from "next/server";
import { findOneWithDecryption, findWithDecryption } from "@open-mercato/shared/lib/encryption/find";
import { InboxProposalAction, InboxDiscrepancy, InboxEmail } from "../../../data/entities.js";
import {
  resolveRequestContext,
  resolveProposal,
  handleRouteError,
  isErrorResponse
} from "../../routeHelpers.js";
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["inbox_ops.proposals.view"] }
};
async function GET(req) {
  try {
    const ctx = await resolveRequestContext(req);
    const proposal = await resolveProposal(new URL(req.url), ctx);
    if (isErrorResponse(proposal)) return proposal;
    const [actions, discrepancies, email] = await Promise.all([
      findWithDecryption(
        ctx.em,
        InboxProposalAction,
        { proposalId: proposal.id, organizationId: ctx.organizationId, tenantId: ctx.tenantId, deletedAt: null },
        { orderBy: { sortOrder: "ASC" } },
        ctx.scope
      ),
      findWithDecryption(
        ctx.em,
        InboxDiscrepancy,
        { proposalId: proposal.id, organizationId: ctx.organizationId, tenantId: ctx.tenantId, deletedAt: null },
        { orderBy: { createdAt: "ASC" } },
        ctx.scope
      ),
      findOneWithDecryption(
        ctx.em,
        InboxEmail,
        {
          id: proposal.inboxEmailId,
          organizationId: ctx.organizationId,
          tenantId: ctx.tenantId,
          deletedAt: null
        },
        void 0,
        ctx.scope
      )
    ]);
    return NextResponse.json({
      proposal: {
        ...proposal,
        actions
      },
      actions,
      discrepancies,
      email
    });
  } catch (err) {
    return handleRouteError(err, "load proposal");
  }
}
const openApi = {
  tag: "InboxOps",
  summary: "Proposal detail",
  methods: {
    GET: {
      summary: "Get proposal detail",
      description: "Returns proposal with actions, discrepancies, and source email",
      responses: [
        { status: 200, description: "Full proposal detail" },
        { status: 404, description: "Proposal not found" }
      ]
    }
  }
};
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
