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 onboardingCaseStatusSchema = z.enum(['pending', 'in_progress', 'completed', 'cancelled']);
export const documentSigningStatusSchema = z.enum(['pending', 'signed', 'rejected']);
export const provisioningStatusSchema = z.enum(['pending', 'requested', 'assigned', 'provisioned', 'confirmed', 'rejected']);

export const createOnboardingTemplateSchema = z.object({
  title: z.string().min(2),
  description: optionalString,
  is_active: z.boolean().optional().default(true),
  is_default: z.boolean().optional().default(false),
  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 createOnboardingTemplateDto = z.infer<typeof createOnboardingTemplateSchema>;

export const updateOnboardingTemplateSchema = createOnboardingTemplateSchema.partial();
export type updateOnboardingTemplateDto = z.infer<typeof updateOnboardingTemplateSchema>;

export const createOnboardingCaseSchema = z.object({
  member_id: z.string().min(1),
  job_application_id: optionalString,
  template_id: optionalString,
  start_date: optionalDate,
  target_completion_date: optionalDate,
  notes: optionalString,
});
export type createOnboardingCaseDto = z.infer<typeof createOnboardingCaseSchema>;

export const updateOnboardingCaseSchema = z.object({
  status: onboardingCaseStatusSchema.optional(),
  start_date: optionalDate,
  target_completion_date: optionalDate,
  notes: optionalString,
});
export type updateOnboardingCaseDto = z.infer<typeof updateOnboardingCaseSchema>;

export const createOnboardingTaskSchema = 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 createOnboardingTaskDto = z.infer<typeof createOnboardingTaskSchema>;

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

export const signOnboardingDocumentSchema = z.object({
  file_url: optionalString,
  signature_metadata: z.record(z.string(), z.any()).optional(),
  notes: optionalString,
});
export type signOnboardingDocumentDto = z.infer<typeof signOnboardingDocumentSchema>;

export const createProvisioningItemSchema = z.object({
  item_name: z.string().min(2),
  item_type: z.string().default('hardware'),
  description: optionalString,
  owner_type: taskOwnerTypeEnumSchema.default('it'),
  assigned_member_id: optionalString,
  serial_number: optionalString,
  notes: optionalString,
});
export type createProvisioningItemDto = z.infer<typeof createProvisioningItemSchema>;

export const updateProvisioningItemSchema = z.object({
  status: provisioningStatusSchema.optional(),
  serial_number: optionalString,
  notes: optionalString,
});
export type updateProvisioningItemDto = z.infer<typeof updateProvisioningItemSchema>;

export const assignBuddySchema = z.object({
  buddy_member_id: z.string().min(1),
  start_date: optionalDate,
  end_date: optionalDate,
  notes: optionalString,
});
export type assignBuddyDto = z.infer<typeof assignBuddySchema>;

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

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