"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateOrderSchema = exports.adminOrderListQuerySchema = exports.customerOrderListQuerySchema = exports.createOrderSchema = exports.orderIdParamSchema = void 0;
const zod_1 = require("zod");
const schema_js_1 = require("../db/schema.js");
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());
const enumList = (values) => 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(values)).optional());
exports.orderIdParamSchema = zod_1.z.object({
    id: zod_1.z.coerce.number().int().positive(),
});
exports.createOrderSchema = zod_1.z
    .object({
    items: zod_1.z
        .array(zod_1.z.object({
        productId: zod_1.z.coerce.number().int().positive(),
        quantity: zod_1.z.coerce.number().int().min(1).max(20),
        lensChoice: zod_1.z.enum(["none", "blue_cut", "power", "visit_store"]).default("none"),
        prescriptionUrl: zod_1.z.string().trim().max(500).optional(),
    }))
        .min(1)
        .max(50),
    fulfillmentMethod: zod_1.z.enum(schema_js_1.fulfillmentMethodValues),
    contactPhone: zod_1.z.string().trim().min(7).max(30).optional(),
    deliveryAddress: zod_1.z.string().trim().min(5).max(2000).optional(),
    paymentMethod: zod_1.z.enum(schema_js_1.paymentMethodValues).default("cash_on_delivery"),
    customerNotes: zod_1.z.string().trim().max(2000).optional(),
})
    .refine((value) => value.fulfillmentMethod !== "delivery" || Boolean(value.deliveryAddress), "Delivery address is required for home delivery");
exports.customerOrderListQuerySchema = zod_1.z.object({
    page: zod_1.z.coerce.number().int().min(1).default(1),
    perPage: zod_1.z.coerce.number().int().min(1).max(50).default(10),
    status: enumList(schema_js_1.orderStatusValues),
    paymentStatus: enumList(schema_js_1.paymentStatusValues),
    activeOnly: queryBoolean,
});
exports.adminOrderListQuerySchema = 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(100).default(20),
    status: enumList(schema_js_1.orderStatusValues),
    paymentStatus: enumList(schema_js_1.paymentStatusValues),
    fulfillmentMethod: enumList(schema_js_1.fulfillmentMethodValues),
    from: zod_1.z.coerce.date().optional(),
    to: zod_1.z.coerce.date().optional(),
    sort: zod_1.z.enum(["newest", "oldest", "amount_desc", "amount_asc"]).default("newest"),
});
exports.updateOrderSchema = zod_1.z
    .object({
    status: zod_1.z.enum(schema_js_1.orderStatusValues).optional(),
    paymentStatus: zod_1.z.enum(schema_js_1.paymentStatusValues).optional(),
    managedByStaffId: zod_1.z.coerce.number().int().positive().nullable().optional(),
    staffNotes: zod_1.z.string().trim().max(2000).nullable().optional(),
})
    .refine((value) => Object.keys(value).length > 0, "At least one field is required");
//# sourceMappingURL=orderValidators.js.map