import { z } from 'zod';
import {
  dateTransformSchema,
  defaultTransformSchema,
} from '../shared/shared.dto';

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

const statusSchema = z.enum(['active', 'inactive', 'deleted']);

export const createEmployementTypeSchema = z.object({
  name: z.string().trim().min(2, {
    message: 'Employement type name must be at least 2 characters long',
  }),
  description: optionalString,
  status: statusSchema.optional(),
});

export const updateEmployementTypeSchema = createEmployementTypeSchema
  .partial()
  .refine((data) => Object.keys(data).length > 0, {
    message: 'At least one field must be provided',
  });

export const employementTypeResponseSchema = z.object({
  id: z.string().uuid().optional(),
  name: z.string().optional(),
  description: defaultTransformSchema,
  status: statusSchema.optional(),
  created_at: dateTransformSchema,
  updated_at: dateTransformSchema,
});

export const employementTypesResponseSchema = z.array(
  employementTypeResponseSchema,
);

export type createEmployementTypeDto = z.infer<
  typeof createEmployementTypeSchema
>;
export type updateEmployementTypeDto = z.infer<
  typeof updateEmployementTypeSchema
>;
export type employementTypeResponseDto = z.infer<
  typeof employementTypeResponseSchema
>;
export type employementTypesResponseDto = z.infer<
  typeof employementTypesResponseSchema
>;
