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 taskOwnerTypeEnumSchema = z.enum(['hr', 'it', 'manager', 'employee', 'finance']);
export const priorityEnumSchema = z.enum(['low', 'medium', 'high', 'critical']);
export const taskStatusEnumSchema = z.enum(['pending', 'in_progress', 'completed', 'skipped']);
export const offboardingCaseStatusSchema = z.enum([
  'initiated',
  'in_progress',
  'pending_clearance',
  'pending_settlement',
  'ready_to_close',
  'completed',
  'cancelled',
]);
export const offboardingReasonSchema = z.enum(['resignation', 'termination', 'retirement', 'contract_end', 'other']);
export const settlementStatusSchema = z.enum([
  'draft',
  'calculated',
  'under_review',
  'approved',
  'included_in_payroll',
  'paid',
  'cancelled',
]);

export const createOffboardingTemplateSchema = z.object({
  title: z.string().min(2),
  description: optionalString,
  is_active: z.boolean().optional().default(true),
  tasks: z.array(z.object({
    title: z.string().min(2),
    description: optionalString,
    owner_type: taskOwnerTypeEnumSchema.default('hr'),
    priority: priorityEnumSchema.default('medium'),
    relative_due_days: z.number().int().default(7),
    order_index: z.number().int().default(0),
  })).optional(),
});
export type createOffboardingTemplateDto = z.infer<typeof createOffboardingTemplateSchema>;

export const createOffboardingCaseSchema = z.object({
  member_id: z.string().min(1),
  template_id: optionalString,
  reason: offboardingReasonSchema.default('resignation'),
  last_working_date: optionalDate,
  notice_period_days: z.number().int().min(0).default(30),
  notes: optionalString,
});
export type createOffboardingCaseDto = z.infer<typeof createOffboardingCaseSchema>;

export const updateOffboardingCaseSchema = z.object({
  status: offboardingCaseStatusSchema.optional(),
  last_working_date: optionalDate,
  notice_period_days: z.number().int().optional(),
  notes: optionalString,
});
export type updateOffboardingCaseDto = z.infer<typeof updateOffboardingCaseSchema>;

export const createOffboardingTaskSchema = z.object({
  title: z.string().min(2),
  description: optionalString,
  owner_type: taskOwnerTypeEnumSchema.default('hr'),
  assigned_member_id: optionalString,
  due_date: optionalDate,
  priority: priorityEnumSchema.default('medium'),
  order_index: z.number().int().default(0),
  notes: optionalString,
});
export type createOffboardingTaskDto = z.infer<typeof createOffboardingTaskSchema>;

export const updateOffboardingTaskSchema = z.object({
  title: optionalString,
  description: optionalString,
  owner_type: taskOwnerTypeEnumSchema,
  assigned_member_id: optionalString,
  due_date: optionalDate,
  priority: priorityEnumSchema,
  status: taskStatusEnumSchema,
  notes: optionalString,
});
export type updateOffboardingTaskDto = z.infer<typeof updateOffboardingTaskSchema>;

export const addEquipmentReturnSchema = z.object({
  item_name: z.string().min(2),
  item_type: z.string().default('hardware'),
  serial_number: optionalString,
  condition: z.string().default('good'),
  notes: optionalString,
});
export type addEquipmentReturnDto = z.infer<typeof addEquipmentReturnSchema>;

export const updateEquipmentReturnSchema = z.object({
  condition: z.string().optional(),
  returned_at: optionalDate,
  notes: optionalString,
});
export type updateEquipmentReturnDto = z.infer<typeof updateEquipmentReturnSchema>;

export const createExitInterviewTemplateSchema = z.object({
  title: z.string().min(2),
  description: optionalString,
  is_active: z.boolean().optional().default(true),
  questions: z.array(z.object({
    id: z.string(),
    question: z.string(),
    type: z.enum(['text', 'long_text', 'single_choice', 'multiple_choice', 'rating']),
    options: z.array(z.string()).optional(),
    required: z.boolean().optional(),
  })),
});
export type createExitInterviewTemplateDto = z.infer<typeof createExitInterviewTemplateSchema>;

export const submitExitInterviewSchema = z.object({
  responses: z.record(z.string(), z.any()),
  notes: optionalString,
});
export type submitExitInterviewDto = z.infer<typeof submitExitInterviewSchema>;

export const calculateFinalSettlementSchema = z.object({
  gross_salary: z.number().optional(),
  unused_leave_days: z.number().optional(),
  daily_leave_rate: z.number().optional(),
  severance_amount: z.number().optional(),
  notice_pay_adjustment: z.number().optional(),
  deductions_amount: z.number().optional(),
  notes: optionalString,
});
export type calculateFinalSettlementDto = z.infer<typeof calculateFinalSettlementSchema>;

export const filterOffboardingCaseSchema = 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: offboardingCaseStatusSchema.optional(),
  member_id: optionalString,
});
export type filterOffboardingCaseDto = z.infer<typeof filterOffboardingCaseSchema>;
