import { z } from 'zod';

const optionalString = z.preprocess((val) => {
  if (typeof val === 'string' && val.trim() === '') return undefined;
  return val;
}, z.string().trim().optional());

const optionalDate = z.preprocess((val) => {
  if (val === null || val === undefined || val === '') return undefined;
  return val;
}, z.coerce.date().optional());

export const probationStatusSchema = z.enum(['pending', 'under_review', 'passed', 'extended', 'failed']);
export const probationOutcomeSchema = z.enum(['pass', 'extend', 'fail']);

export const createProbationSchema = z.object({
  member_id: z.string().min(1),
  onboarding_case_id: optionalString,
  start_date: optionalDate,
  duration_months: z.number().int().min(1).default(3),
  manager_id: optionalString,
  notes: optionalString,
});
export type createProbationDto = z.infer<typeof createProbationSchema>;

export const reviewProbationSchema = z.object({
  outcome: probationOutcomeSchema,
  extension_months: z.number().int().min(1).optional(),
  comments: optionalString,
});
export type reviewProbationDto = z.infer<typeof reviewProbationSchema>;

export const filterProbationSchema = z.object({
  page: z.preprocess((val) => Number(val) || 1, z.number().int().min(1).default(1)),
  limit: z.preprocess((val) => Number(val) || 10, z.number().int().min(1).max(100).default(10)),
  search: optionalString,
  status: probationStatusSchema.optional(),
  member_id: optionalString,
});
export type filterProbationDto = z.infer<typeof filterProbationSchema>;
