"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateProductSchema = exports.createProductSchema = exports.productIdParamSchema = exports.productSlugParamSchema = exports.productListQuerySchema = void 0;
const zod_1 = require("zod");
const schema_js_1 = require("../db/schema.js");
const positiveId = zod_1.z.coerce.number().int().positive();
const optionalPositiveId = zod_1.z.coerce
    .number()
    .int()
    .positive()
    .nullable()
    .optional();
const slug = zod_1.z
    .string()
    .trim()
    .min(1)
    .max(280)
    .regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/, "Use a URL-safe slug");
const slugList = zod_1.z.preprocess((value) => {
    if (typeof value === "string")
        return value
            .split(",")
            .map((item) => item.trim())
            .filter(Boolean);
    return value;
}, zod_1.z.array(zod_1.z.string().trim().min(1).max(160)).optional());
const numberList = zod_1.z.array(zod_1.z.coerce.number().int().positive()).default([]);
const queryBoolean = zod_1.z.preprocess((value) => {
    if (value === undefined)
        return undefined;
    if (typeof value === "boolean")
        return value;
    if (typeof value === "string") {
        if (["true", "1", "yes"].includes(value.toLowerCase()))
            return true;
        if (["false", "0", "no"].includes(value.toLowerCase()))
            return false;
    }
    return value;
}, zod_1.z.boolean().optional());
exports.productListQuerySchema = zod_1.z.object({
    q: zod_1.z.string().trim().min(1).max(120).optional(),
    page: zod_1.z.coerce.number().int().min(1).default(1),
    perPage: zod_1.z.coerce.number().int().min(1).max(60).default(12),
    sort: zod_1.z
        .enum([
        "newest",
        "oldest",
        "price_asc",
        "price_desc",
        "rating_desc",
        "popular",
        "name_asc",
    ])
        .default("newest"),
    brand: slugList,
    type: slugList,
    collection: slugList,
    frameShape: slugList,
    faceShape: slugList,
    gender: slugList,
    color: slugList,
    lens: slugList,
    occasion: slugList,
    ageGroup: slugList,
    size: slugList,
    status: zod_1.z.preprocess((value) => {
        if (typeof value === "string")
            return value
                .split(",")
                .map((item) => item.trim())
                .filter(Boolean);
        return value;
    }, zod_1.z.array(zod_1.z.enum(schema_js_1.productStatusValues)).optional()),
    minPrice: zod_1.z.coerce.number().int().min(0).optional(),
    maxPrice: zod_1.z.coerce.number().int().min(0).optional(),
    rating: zod_1.z.coerce.number().min(1).max(5).optional(),
    inStock: queryBoolean,
    featured: queryBoolean,
});
exports.productSlugParamSchema = zod_1.z.object({
    slug,
});
exports.productIdParamSchema = zod_1.z.object({
    id: positiveId,
});
const mediaInputSchema = zod_1.z.object({
    url: zod_1.z.string().trim().min(1).max(512),
    storageKey: zod_1.z.string().trim().max(1024).nullable().optional(),
    fileName: zod_1.z.string().trim().max(255).nullable().optional(),
    mimeType: zod_1.z.string().trim().max(120).nullable().optional(),
    sizeBytes: zod_1.z.coerce.number().int().min(0).nullable().optional(),
    altText: zod_1.z.string().trim().max(255).nullable().optional(),
    sortOrder: zod_1.z.coerce.number().int().min(0).default(0),
    isPrimary: zod_1.z.boolean().default(false),
});
exports.createProductSchema = zod_1.z.object({
    sku: zod_1.z.string().trim().min(1).max(80),
    name: zod_1.z.string().trim().min(2).max(255),
    slug: slug.optional(),
    description: zod_1.z.string().trim().max(5000).nullable().optional(),
    brandId: optionalPositiveId,
    productTypeId: optionalPositiveId,
    collectionId: optionalPositiveId,
    frameShapeId: optionalPositiveId,
    frameColorId: optionalPositiveId,
    ageGroupId: optionalPositiveId,
    sizeId: optionalPositiveId,
    basePrice: zod_1.z.coerce.number().int().min(0),
    discountPercent: zod_1.z.coerce.number().int().min(0).max(100).default(0),
    salePrice: zod_1.z.coerce.number().int().min(0).optional(),
    stockQuantity: zod_1.z.coerce.number().int().min(0).default(0),
    status: zod_1.z.enum(schema_js_1.productStatusValues).default("draft"),
    isFeatured: zod_1.z.boolean().default(false),
    genderIds: numberList,
    faceShapeIds: numberList,
    lensFeatureIds: numberList,
    occasionIds: numberList,
    media: zod_1.z.array(mediaInputSchema).default([]),
});
exports.updateProductSchema = exports.createProductSchema
    .partial()
    .extend({
    genderIds: numberList.optional(),
    faceShapeIds: numberList.optional(),
    lensFeatureIds: numberList.optional(),
    occasionIds: numberList.optional(),
    media: zod_1.z.array(mediaInputSchema).optional(),
})
    .refine((value) => Object.keys(value).length > 0, "At least one field is required");
//# sourceMappingURL=productValidators.js.map