import { z } from "zod";
import { makeCrudRoute } from "@open-mercato/shared/lib/crud/factory";
import { CrudHttpError } from "@open-mercato/shared/lib/crud/errors";
import { CustomerTag } from "../../data/entities.js";
import { tagCreateSchema, tagUpdateSchema } from "../../data/validators.js";
import { E } from "../../../../generated/entities.ids.generated.js";
import { resolveTranslations } from "@open-mercato/shared/lib/i18n/server";
import { withScopedPayload } from "../utils.js";
import {
  createCustomersCrudOpenApi,
  createPagedListResponseSchema,
  defaultOkResponseSchema
} from "../openapi.js";
import { escapeLikePattern } from "@open-mercato/shared/lib/db/escapeLikePattern";
const rawBodySchema = z.object({}).passthrough();
const listSchema = z.object({
  page: z.coerce.number().min(1).default(1),
  pageSize: z.coerce.number().min(1).max(100).default(100),
  search: z.string().optional(),
  sortField: z.string().optional(),
  sortDir: z.enum(["asc", "desc"]).optional()
}).passthrough();
const routeMetadata = {
  GET: { requireAuth: true, requireFeatures: ["customers.activities.view"] },
  POST: { requireAuth: true, requireFeatures: ["customers.activities.manage"] },
  PUT: { requireAuth: true, requireFeatures: ["customers.activities.manage"] },
  DELETE: { requireAuth: true, requireFeatures: ["customers.activities.manage"] }
};
const metadata = routeMetadata;
const crud = makeCrudRoute({
  metadata: routeMetadata,
  orm: {
    entity: CustomerTag,
    idField: "id",
    orgField: "organizationId",
    tenantField: "tenantId",
    // CustomerTag has no `deleted_at` column. Without this, the auto-registered
    // generic optimistic-lock reader would filter on `deletedAt: null` against a
    // non-existent column, throw, and fail open — silently disabling the lock.
    softDeleteField: null
  },
  indexer: { entityType: E.customers.customer_tag },
  list: {
    schema: listSchema,
    entityId: E.customers.customer_tag,
    fields: ["id", "slug", "label", "color", "description", "organization_id", "tenant_id", "updated_at"],
    buildFilters: async (query) => {
      const filters = {};
      if (query.search) {
        filters.label = { $ilike: `%${escapeLikePattern(query.search)}%` };
      }
      return filters;
    }
  },
  actions: {
    create: {
      commandId: "customers.tags.create",
      schema: rawBodySchema,
      mapInput: async ({ raw, ctx }) => {
        const { translate } = await resolveTranslations();
        return tagCreateSchema.parse(withScopedPayload(raw ?? {}, ctx, translate));
      },
      response: ({ result }) => ({ id: result?.tagId ?? result?.id ?? null }),
      status: 201
    },
    update: {
      commandId: "customers.tags.update",
      schema: rawBodySchema,
      mapInput: async ({ raw, ctx }) => {
        const { translate } = await resolveTranslations();
        return tagUpdateSchema.parse(withScopedPayload(raw ?? {}, ctx, translate));
      },
      response: () => ({ ok: true })
    },
    delete: {
      commandId: "customers.tags.delete",
      schema: rawBodySchema,
      mapInput: async ({ parsed, ctx }) => {
        const { translate } = await resolveTranslations();
        const id = parsed?.body?.id ?? parsed?.id ?? parsed?.query?.id ?? (ctx.request ? new URL(ctx.request.url).searchParams.get("id") : null);
        if (!id) throw new CrudHttpError(400, { error: translate("customers.errors.tag_required", "Tag id is required") });
        return { id };
      },
      response: () => ({ ok: true })
    }
  }
});
const { POST, PUT, DELETE } = crud;
const GET = crud.GET;
const tagListItemSchema = z.object({
  id: z.string().uuid(),
  slug: z.string(),
  label: z.string(),
  color: z.string().nullable().optional(),
  description: z.string().nullable().optional(),
  organization_id: z.string().uuid().nullable().optional(),
  tenant_id: z.string().uuid().nullable().optional(),
  updated_at: z.string().nullable().optional()
});
const tagCreateResponseSchema = z.object({
  id: z.string().uuid().nullable()
});
const openApi = createCustomersCrudOpenApi({
  resourceName: "Tag",
  querySchema: listSchema,
  listResponseSchema: createPagedListResponseSchema(tagListItemSchema),
  create: {
    schema: tagCreateSchema,
    responseSchema: tagCreateResponseSchema,
    description: "Creates a tag scoped to the current tenant and organization."
  },
  update: {
    schema: tagUpdateSchema,
    responseSchema: defaultOkResponseSchema,
    description: "Updates label, color, or description for an existing tag."
  },
  del: {
    schema: z.object({ id: z.string().uuid() }),
    responseSchema: defaultOkResponseSchema,
    description: "Deletes a tag identified by `id`. The identifier may be provided via body or query string."
  }
});
export {
  DELETE,
  GET,
  POST,
  PUT,
  metadata,
  openApi
};
//# sourceMappingURL=route.js.map
