import { NextResponse } from "next/server";
import { z } from "zod";
import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
import { CrudHttpError, isCrudHttpError } from "@open-mercato/shared/lib/crud/errors";
import { CustomerEntity } from "../../../../data/entities.js";
import { resolveWidgetScope } from "../utils.js";
import { parseBooleanToken } from "@open-mercato/shared/lib/boolean";
import { createLogger } from "@open-mercato/shared/lib/logger";
const logger = createLogger("customers");
const querySchema = z.object({
  limit: z.coerce.number().min(1).max(20).default(5),
  tenantId: z.string().uuid().optional(),
  organizationId: z.string().uuid().optional(),
  includePast: z.enum(["true", "false"]).optional()
});
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["dashboards.view", "customers.widgets.next-interactions"] }
};
async function resolveContext(req, translate) {
  const url = new URL(req.url);
  const rawQuery = {};
  for (const [key, value] of url.searchParams.entries()) {
    rawQuery[key] = value;
  }
  const parsed = querySchema.safeParse(rawQuery);
  if (!parsed.success) {
    throw new CrudHttpError(400, { error: translate("customers.errors.invalid_query", "Invalid query parameters") });
  }
  const { container, em, tenantId, organizationIds } = await resolveWidgetScope(req, translate, {
    tenantId: parsed.data.tenantId ?? null,
    organizationId: parsed.data.organizationId ?? null
  });
  return {
    container,
    em,
    tenantId,
    organizationIds,
    limit: parsed.data.limit,
    includePast: parseBooleanToken(parsed.data.includePast) === true
  };
}
async function GET(req) {
  const { translate } = await resolveTranslations();
  try {
    const { em, tenantId, organizationIds, limit, includePast } = await resolveContext(req, translate);
    const organizationFilter = Array.isArray(organizationIds) ? organizationIds.length === 1 ? organizationIds[0] : { $in: Array.from(new Set(organizationIds)) } : null;
    const now = /* @__PURE__ */ new Date();
    const filters = {
      tenantId,
      deletedAt: null,
      nextInteractionAt: includePast ? { $ne: null } : { $gte: now }
    };
    if (organizationFilter) filters.organizationId = organizationFilter;
    const entities = await em.find(CustomerEntity, filters, {
      limit,
      orderBy: { nextInteractionAt: "asc" }
    });
    const items = entities.map((entity) => ({
      id: entity.id,
      displayName: entity.displayName,
      kind: entity.kind,
      organizationId: entity.organizationId,
      nextInteractionAt: entity.nextInteractionAt ? entity.nextInteractionAt.toISOString() : null,
      nextInteractionName: entity.nextInteractionName ?? null,
      nextInteractionIcon: entity.nextInteractionIcon ?? null,
      nextInteractionColor: entity.nextInteractionColor ?? null,
      ownerUserId: entity.ownerUserId ?? null
    }));
    return NextResponse.json({ items, now: now.toISOString() });
  } catch (err) {
    if (isCrudHttpError(err)) {
      return NextResponse.json(err.body, { status: err.status });
    }
    logger.error("customers.widgets.nextInteractions failed", { err });
    return NextResponse.json(
      { error: translate("customers.widgets.nextInteractions.error", "Failed to load upcoming interactions") },
      { status: 500 }
    );
  }
}
const nextInteractionItemSchema = z.object({
  id: z.string().uuid(),
  displayName: z.string().nullable().optional(),
  kind: z.string().nullable().optional(),
  organizationId: z.string().uuid().nullable().optional(),
  nextInteractionAt: z.string().nullable(),
  nextInteractionName: z.string().nullable().optional(),
  nextInteractionIcon: z.string().nullable().optional(),
  nextInteractionColor: z.string().nullable().optional(),
  ownerUserId: z.string().uuid().nullable().optional()
});
const nextInteractionResponseSchema = z.object({
  items: z.array(nextInteractionItemSchema),
  now: z.string()
});
const widgetErrorSchema = z.object({
  error: z.string()
});
const openApi = {
  tag: "Customers",
  summary: "Next interactions widget",
  methods: {
    GET: {
      summary: "Fetch upcoming customer interactions",
      description: "Lists upcoming (or optionally past) customer interaction reminders ordered by interaction date.",
      query: querySchema,
      responses: [
        { status: 200, description: "Widget payload", schema: nextInteractionResponseSchema }
      ],
      errors: [
        { status: 400, description: "Invalid query parameters", schema: widgetErrorSchema },
        { status: 401, description: "Unauthorized", schema: widgetErrorSchema },
        { status: 500, description: "Widget failed to load", schema: widgetErrorSchema }
      ]
    }
  }
};
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
