import { NextResponse } from "next/server";
import { z } from "zod";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { resolveOrganizationScopeForRequest } from "@open-mercato/core/modules/directory/utils/organizationScope";
import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
import { CrudHttpError, isCrudHttpError } from "@open-mercato/shared/lib/crud/errors";
import { readJsonSafe } from "@open-mercato/shared/lib/http/readJsonSafe";
import {
  runCrudMutationGuardAfterSuccess,
  validateCrudMutationGuard
} from "@open-mercato/shared/lib/crud/mutation-guard";
import {
  customerStuckThresholdUpsertSchema
} from "../../../data/validators.js";
import { loadCustomerSettings } from "../../../commands/settings.js";
import { withScopedPayload } from "../../utils.js";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("customers");
const DEFAULT_THRESHOLD = 14;
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["customers.deals.manage"] },
  PUT: { requireAuth: true, requireFeatures: ["customers.deals.manage"] }
};
async function resolveSettingsContext(req) {
  const container = await createRequestContainer();
  const auth = await getAuthFromRequest(req);
  const { translate } = await resolveTranslations();
  if (!auth || !auth.tenantId) {
    throw new CrudHttpError(401, {
      error: translate("customers.errors.unauthorized", "Unauthorized")
    });
  }
  const scope = await resolveOrganizationScopeForRequest({ container, auth, request: req });
  const organizationId = scope?.selectedId ?? auth.orgId ?? null;
  if (!organizationId) {
    throw new CrudHttpError(400, {
      error: translate("customers.errors.organization_required", "Organization context is required")
    });
  }
  const ctx = {
    container,
    auth,
    organizationScope: scope,
    selectedOrganizationId: organizationId,
    organizationIds: scope?.filterIds ?? (auth.orgId ? [auth.orgId] : null),
    request: req
  };
  const em = container.resolve("em");
  return {
    ctx,
    tenantId: auth.tenantId,
    organizationId,
    translate,
    em
  };
}
async function GET(req) {
  try {
    const { em, tenantId, organizationId } = await resolveSettingsContext(req);
    const record = await loadCustomerSettings(em, { tenantId, organizationId });
    return NextResponse.json({
      stuckThresholdDays: record?.stuckThresholdDays ?? DEFAULT_THRESHOLD
    });
  } catch (err) {
    if (isCrudHttpError(err)) {
      return NextResponse.json(err.body, { status: err.status });
    }
    const { translate } = await resolveTranslations();
    logger.error("customers.settings.stuck-threshold.get failed", { err });
    return NextResponse.json(
      { error: translate("customers.errors.lookup_failed", "Failed to load settings") },
      { status: 400 }
    );
  }
}
async function PUT(req) {
  try {
    const { ctx, tenantId, organizationId, translate } = await resolveSettingsContext(req);
    const payload = await readJsonSafe(req, {});
    const scoped = withScopedPayload(payload, ctx, translate);
    const input = customerStuckThresholdUpsertSchema.parse(scoped);
    const guardResult = await validateCrudMutationGuard(ctx.container, {
      tenantId,
      organizationId,
      // `resolveSettingsContext` throws 401 when auth is missing, so by the time we reach
      // here `ctx.auth.sub` is guaranteed to be a string per `AuthContext`.
      userId: ctx.auth.sub,
      resourceKind: "customers.settings",
      resourceId: organizationId,
      operation: "update",
      requestMethod: req.method,
      requestHeaders: req.headers,
      mutationPayload: input
    });
    if (guardResult && !guardResult.ok) {
      return NextResponse.json(guardResult.body, { status: guardResult.status });
    }
    const commandBus = ctx.container.resolve("commandBus");
    const { result } = await commandBus.execute("customers.settings.save_stuck_threshold", { input, ctx });
    if (guardResult?.ok && guardResult.shouldRunAfterSuccess) {
      await runCrudMutationGuardAfterSuccess(ctx.container, {
        tenantId,
        organizationId,
        // `resolveSettingsContext` throws 401 when auth is missing, so by the time we reach
        // here `ctx.auth.sub` is guaranteed to be a string per `AuthContext`.
        userId: ctx.auth.sub,
        resourceKind: "customers.settings",
        resourceId: organizationId,
        operation: "update",
        requestMethod: req.method,
        requestHeaders: req.headers,
        metadata: guardResult.metadata ?? null
      });
    }
    return NextResponse.json({
      stuckThresholdDays: result?.stuckThresholdDays ?? input.stuckThresholdDays
    });
  } catch (err) {
    if (isCrudHttpError(err)) {
      return NextResponse.json(err.body, { status: err.status });
    }
    const { translate } = await resolveTranslations();
    logger.error("customers.settings.stuck-threshold.put failed", { err });
    return NextResponse.json(
      { error: translate("customers.errors.save_failed", "Failed to save settings") },
      { status: 400 }
    );
  }
}
const stuckThresholdResponseSchema = z.object({
  stuckThresholdDays: z.number()
});
const stuckThresholdErrorSchema = z.object({
  error: z.string()
});
const openApi = {
  tag: "Customers",
  summary: "Customer deals stuck-threshold setting",
  methods: {
    GET: {
      summary: "Retrieve stuck-threshold days",
      description: "Returns the current stuck-deal threshold (in days) for the selected organization.",
      responses: [
        { status: 200, description: "Current threshold", schema: stuckThresholdResponseSchema },
        { status: 401, description: "Unauthorized", schema: stuckThresholdErrorSchema },
        { status: 400, description: "Organization context missing", schema: stuckThresholdErrorSchema }
      ]
    },
    PUT: {
      summary: "Update stuck-threshold days",
      description: "Updates the stuck-deal threshold for the selected organization.",
      requestBody: {
        contentType: "application/json",
        schema: customerStuckThresholdUpsertSchema
      },
      responses: [
        { status: 200, description: "Updated threshold", schema: stuckThresholdResponseSchema },
        { status: 401, description: "Unauthorized", schema: stuckThresholdErrorSchema },
        { status: 400, description: "Invalid payload", schema: stuckThresholdErrorSchema }
      ]
    }
  }
};
export {
  GET,
  PUT,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
