import { NextResponse } from "next/server";
import { z } from "zod";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { CustomerUser } from "@open-mercato/core/modules/customer_accounts/data/entities";
import { emitCustomerAccountsEvent } from "@open-mercato/core/modules/customer_accounts/events";
import { findOneWithDecryption } from "@open-mercato/shared/lib/encryption/find";
const metadata = {};
async function POST(req, { params }) {
  const auth = await getAuthFromRequest(req);
  if (!auth) {
    return NextResponse.json({ ok: false, error: "Authentication required" }, { status: 401 });
  }
  const container = await createRequestContainer();
  const rbacService = container.resolve("rbacService");
  const hasAccess = await rbacService.userHasAllFeatures(auth.sub, ["customer_accounts.manage"], { tenantId: auth.tenantId, organizationId: auth.orgId });
  if (!hasAccess) {
    return NextResponse.json({ ok: false, error: "Insufficient permissions" }, { status: 403 });
  }
  const em = container.resolve("em");
  const user = await findOneWithDecryption(
    em,
    CustomerUser,
    {
      id: params.id,
      tenantId: auth.tenantId,
      organizationId: auth.orgId,
      deletedAt: null
    },
    void 0,
    { tenantId: auth.tenantId, organizationId: auth.orgId }
  );
  if (!user) {
    return NextResponse.json({ ok: false, error: "User not found" }, { status: 404 });
  }
  if (user.emailVerifiedAt) {
    return NextResponse.json({ ok: true, alreadyVerified: true });
  }
  await em.nativeUpdate(CustomerUser, {
    id: user.id,
    tenantId: user.tenantId,
    organizationId: user.organizationId
  }, { emailVerifiedAt: /* @__PURE__ */ new Date() });
  void emitCustomerAccountsEvent("customer_accounts.email.verified", {
    id: user.id,
    recipientUserId: user.id,
    email: user.email,
    tenantId: user.tenantId,
    organizationId: user.organizationId,
    verifiedBy: auth.sub
  }).catch(() => void 0);
  return NextResponse.json({ ok: true });
}
const successSchema = z.object({ ok: z.literal(true), alreadyVerified: z.boolean().optional() });
const errorSchema = z.object({ ok: z.literal(false), error: z.string() });
const methodDoc = {
  summary: "Verify customer user email (admin)",
  description: "Allows staff to manually mark a customer user email as verified.",
  tags: ["Customer Accounts Admin"],
  responses: [{ status: 200, description: "Email verified", schema: successSchema }],
  errors: [
    { status: 401, description: "Not authenticated", schema: errorSchema },
    { status: 403, description: "Insufficient permissions", schema: errorSchema },
    { status: 404, description: "User not found", schema: errorSchema }
  ]
};
const openApi = {
  summary: "Verify customer user email (admin)",
  pathParams: z.object({ id: z.string().uuid() }),
  methods: { POST: methodDoc }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=verify-email.js.map
