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 { adminResetPasswordSchema } from "@open-mercato/core/modules/customer_accounts/data/validators";
import { emitCustomerAccountsEvent } from "@open-mercato/core/modules/customer_accounts/events";
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 });
  }
  let body;
  try {
    body = await req.json();
  } catch {
    return NextResponse.json({ ok: false, error: "Invalid request body" }, { status: 400 });
  }
  const parsed = adminResetPasswordSchema.safeParse(body);
  if (!parsed.success) {
    return NextResponse.json({ ok: false, error: "Validation failed", details: parsed.error.flatten().fieldErrors }, { status: 400 });
  }
  const customerUserService = container.resolve("customerUserService");
  const customerSessionService = container.resolve("customerSessionService");
  const em = container.resolve("em");
  const user = await customerUserService.findById(params.id, auth.tenantId, auth.orgId);
  if (!user) {
    return NextResponse.json({ ok: false, error: "User not found" }, { status: 404 });
  }
  await em.transactional(async (trx) => {
    await customerUserService.updatePassword(user, parsed.data.newPassword, trx);
    await customerSessionService.revokeAllUserSessions(user.id, trx);
  });
  void emitCustomerAccountsEvent("customer_accounts.password.reset", {
    id: user.id,
    recipientUserId: user.id,
    email: user.email,
    tenantId: user.tenantId,
    organizationId: user.organizationId,
    resetBy: auth.sub
  }).catch(() => void 0);
  void emitCustomerAccountsEvent("customer_accounts.password.changed", {
    userId: user.id,
    tenantId: user.tenantId,
    organizationId: user.organizationId,
    changedBy: "admin",
    changedById: auth.sub,
    at: (/* @__PURE__ */ new Date()).toISOString()
  }).catch(() => void 0);
  return NextResponse.json({ ok: true });
}
const successSchema = z.object({ ok: z.literal(true) });
const errorSchema = z.object({ ok: z.literal(false), error: z.string() });
const methodDoc = {
  summary: "Reset customer user password (admin)",
  description: "Allows staff to set a new password for a customer user.",
  tags: ["Customer Accounts Admin"],
  requestBody: { schema: adminResetPasswordSchema },
  responses: [{ status: 200, description: "Password reset", schema: successSchema }],
  errors: [
    { status: 400, description: "Validation failed", schema: errorSchema },
    { 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: "Reset customer user password (admin)",
  pathParams: z.object({ id: z.string().uuid() }),
  methods: { POST: methodDoc }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=reset-password.js.map
