"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.holidaysResponseSchema = exports.holidayResponseSchema = exports.holidayFilterSchema = exports.updateHolidaySchema = exports.createHolidaySchema = void 0;
const zod_1 = require("zod");
const shared_dto_1 = require("../shared/shared.dto");
const optionalString = zod_1.z.preprocess((value) => {
    if (typeof value === 'string' && value.trim() === '')
        return undefined;
    return value;
}, zod_1.z.string().trim().optional());
const optionalDate = zod_1.z.preprocess((value) => {
    if (value === null || value === undefined || value === '')
        return undefined;
    return value;
}, zod_1.z.coerce.date().optional());
const optionalBool = zod_1.z.preprocess((value) => {
    if (value === null || value === undefined || value === '')
        return undefined;
    if (typeof value === 'boolean')
        return value;
    if (typeof value === 'string') {
        const normalized = value.trim().toLowerCase();
        if (normalized === 'true')
            return true;
        if (normalized === 'false')
            return false;
    }
    return value;
}, zod_1.z.boolean().optional());
const holidayTypeSchema = zod_1.z.enum([
    'public',
    'company',
    'optional',
    'religious',
    'regional',
    'observance',
    'other',
]);
const departmentPreviewSchema = zod_1.z.object({
    id: zod_1.z.string().uuid(),
    name: zod_1.z.string(),
    department_code: zod_1.z.string(),
});
exports.createHolidaySchema = zod_1.z
    .object({
    name: zod_1.z.string().trim().min(2, { message: 'Holiday name is required' }),
    holiday_type: holidayTypeSchema.optional(),
    start_date: zod_1.z.coerce.date({ message: 'Start date is required' }),
    end_date: zod_1.z.coerce.date({ message: 'End date is required' }),
    is_recurring: optionalBool,
    department_ids: zod_1.z.array(zod_1.z.string().uuid()).optional(),
    description: optionalString,
})
    .refine((data) => data.end_date >= data.start_date, {
    path: ['end_date'],
    message: 'End date must be after or equal to start date',
});
exports.updateHolidaySchema = zod_1.z
    .object({
    name: optionalString,
    holiday_type: holidayTypeSchema.optional(),
    start_date: optionalDate,
    end_date: optionalDate,
    is_recurring: optionalBool,
    department_ids: zod_1.z.array(zod_1.z.string().uuid()).optional(),
    description: optionalString,
})
    .refine((data) => Object.keys(data).length > 0, {
    message: 'At least one field must be provided',
})
    .refine((data) => !data.start_date || !data.end_date || data.end_date >= data.start_date, {
    path: ['end_date'],
    message: 'End date must be after or equal to start date',
});
exports.holidayFilterSchema = zod_1.z.object({
    start_date: optionalDate,
    end_date: optionalDate,
    from: optionalDate,
    to: optionalDate,
    holiday_type: holidayTypeSchema.optional(),
    department_id: zod_1.z.preprocess((value) => {
        if (typeof value === 'string' && value.trim() === '')
            return undefined;
        return value;
    }, zod_1.z.string().uuid().optional()),
    is_recurring: optionalBool,
    search: zod_1.z.string().trim().optional(),
    page: zod_1.z.preprocess((value) => {
        if (value === undefined || value === null || value === '')
            return 1;
        const parsed = Number(value);
        return Number.isNaN(parsed) ? value : parsed;
    }, zod_1.z.number().int().min(1).default(1)),
    limit: zod_1.z.preprocess((value) => {
        if (value === undefined || value === null || value === '')
            return 10;
        const parsed = Number(value);
        return Number.isNaN(parsed) ? value : parsed;
    }, zod_1.z.number().int().min(1).max(100).default(10)),
    data_scope: zod_1.z.enum(['own', 'all']).optional(),
});
exports.holidayResponseSchema = zod_1.z.object({
    id: zod_1.z.string().uuid().optional(),
    name: zod_1.z.string().optional(),
    holiday_type: holidayTypeSchema.optional(),
    start_date: zod_1.z
        .date()
        .transform((date) => date.toISOString())
        .optional(),
    end_date: zod_1.z
        .date()
        .transform((date) => date.toISOString())
        .optional(),
    occurrence_date: zod_1.z.string().optional(),
    is_recurring: zod_1.z.boolean().optional(),
    generated_from_recurring: zod_1.z.boolean().optional(),
    description: shared_dto_1.defaultTransformSchema,
    departments: zod_1.z.array(departmentPreviewSchema).optional(),
    created_at: shared_dto_1.dateTransformSchema,
    updated_at: shared_dto_1.dateTransformSchema,
});
exports.holidaysResponseSchema = zod_1.z.array(exports.holidayResponseSchema);
//# sourceMappingURL=holidays.dto.js.map