"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.eventsResponseSchema = exports.eventResponseSchema = exports.eventFilterSchema = exports.updateEventSchema = exports.createEventSchema = void 0;
const zod_1 = require("zod");
const shared_dto_1 = require("../shared/shared.dto");
const optionalFileSchema = zod_1.z
    .any()
    .transform((value) => {
    if (!value ||
        typeof value !== 'object' ||
        !('file_name' in value) ||
        typeof value.file_name !== 'string' ||
        value.file_name.trim() === '') {
        return undefined;
    }
    const host = process.env.SERVER_HOST ?? '';
    const hasHost = host.length > 0 && value.file_name.startsWith(host);
    return {
        ...value,
        file_name: hasHost ? value.file_name : `${host}/${value.file_name}`,
    };
})
    .optional();
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 eventTypeSchema = zod_1.z.enum([
    'meeting',
    'training',
    'workshop',
    'conference',
    'social',
    'other',
]);
const eventMemberSchema = zod_1.z.object({
    id: zod_1.z.string().uuid(),
    employee_id: zod_1.z.string(),
    first_name: zod_1.z.string(),
    last_name: shared_dto_1.defaultTransformSchema,
    nickname: shared_dto_1.defaultTransformSchema,
    image: zod_1.z.any().optional(),
});
exports.createEventSchema = zod_1.z
    .object({
    title: zod_1.z.string().trim().min(2, { message: 'Event title is required' }),
    image: optionalFileSchema,
    category: eventTypeSchema.optional(),
    date: zod_1.z.coerce.date({ message: 'Event date is required' }),
    start_time: zod_1.z.coerce.date({ message: 'Start time is required' }),
    end_time: zod_1.z.coerce.date({ message: 'End time is required' }),
    location: optionalString,
    event_owner: optionalString,
    description: optionalString,
    member_ids: zod_1.z.array(zod_1.z.string().uuid()).optional(),
})
    .superRefine((data, ctx) => {
    if (data.end_time <= data.start_time) {
        ctx.addIssue({
            code: zod_1.z.ZodIssueCode.custom,
            path: ['end_time'],
            message: 'End time must be after start time',
        });
    }
});
exports.updateEventSchema = zod_1.z
    .object({
    title: optionalString,
    image: optionalFileSchema.nullable(),
    category: eventTypeSchema.optional(),
    date: optionalDate,
    start_time: optionalDate,
    end_time: optionalDate,
    location: optionalString,
    event_owner: optionalString,
    description: optionalString,
    member_ids: zod_1.z.array(zod_1.z.string().uuid()).optional(),
})
    .refine((data) => Object.keys(data).length > 0, {
    message: 'At least one field must be provided',
})
    .superRefine((data, ctx) => {
    if (data.start_time && data.end_time && data.end_time <= data.start_time) {
        ctx.addIssue({
            code: zod_1.z.ZodIssueCode.custom,
            path: ['end_time'],
            message: 'End time must be after start time',
        });
    }
});
exports.eventFilterSchema = zod_1.z.object({
    category: eventTypeSchema.optional(),
    search: zod_1.z.string().trim().optional(),
    from: optionalDate,
    to: optionalDate,
    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)),
});
exports.eventResponseSchema = zod_1.z.object({
    id: zod_1.z.string().uuid().optional(),
    title: zod_1.z.string().optional(),
    image: zod_1.z.any().optional(),
    category: eventTypeSchema.optional(),
    date: zod_1.z
        .date()
        .transform((date) => date.toISOString())
        .optional(),
    start_time: zod_1.z
        .date()
        .transform((date) => date.toISOString())
        .optional(),
    end_time: zod_1.z
        .date()
        .transform((date) => date.toISOString())
        .optional(),
    location: shared_dto_1.defaultTransformSchema,
    attendees: zod_1.z.number().optional(),
    event_owner: shared_dto_1.defaultTransformSchema,
    description: shared_dto_1.defaultTransformSchema,
    members: zod_1.z.array(eventMemberSchema).optional(),
    created_at: shared_dto_1.dateTransformSchema,
    updated_at: shared_dto_1.dateTransformSchema,
});
exports.eventsResponseSchema = zod_1.z.array(exports.eventResponseSchema);
//# sourceMappingURL=events.dto.js.map