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 createDesignationSchema = z.object({
  name: z
    .string()
    .trim()
    .min(2, { message: 'Designation name must be at least 2 characters long' }),
  designation_code: z
    .string()
    .trim()
    .min(1, { message: 'Designation code is required' }),
  description: optionalString,
  status: statusSchema.optional(),
});

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

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

export const designationsResponseSchema = z.array(designationResponseSchema);

export type createDesignationDto = z.infer<typeof createDesignationSchema>;
export type updateDesignationDto = z.infer<typeof updateDesignationSchema>;
export type designationResponseDto = z.infer<typeof designationResponseSchema>;
export type designationsResponseDto = z.infer<
  typeof designationsResponseSchema
>;
