import z from 'zod';

const optionalString = z.preprocess((value) => {
  if (typeof value === 'string' && value.trim() === '') return undefined;
  return value;
}, z.string().trim().optional());
const optionalDate = z.preprocess((value) => {
  if (value === null || value === undefined || value === '') return undefined;
  return value;
}, z.coerce.date().optional());

export const notificationFilterSchema = z.object({
  start_date: optionalString, // Accept date as string in query params
  end_date: optionalString,
  page: z.preprocess((value) => {
    if (value === undefined || value === null || value === '') return 1;
    const parsed = Number(value);
    return Number.isNaN(parsed) ? value : parsed;
  }, z.number().int().min(1).default(1)),
  limit: z.preprocess((value) => {
    if (value === undefined || value === null || value === '') return 10;
    const parsed = Number(value);
    return Number.isNaN(parsed) ? value : parsed;
  }, z.number().int().min(1).max(100).default(10)),
});

export const notificationMarkAsReadSchema = z.object({
  notification_id: z.string().uuid(),
  member_id: z.string().uuid(),
});

export type NotificationFilterDto = z.infer<typeof notificationFilterSchema>;
export type NotificationMarkAsReadDto = z.infer<
  typeof notificationMarkAsReadSchema
>;
