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 createHiringSourceSchema = z.object({
  name: z.string().trim().min(2, {
    message: 'Hiring source name must be at least 2 characters long',
  }),
  description: optionalString,
  status: statusSchema.optional(),
});

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

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

export const hiringSourcesResponseSchema = z.array(hiringSourceResponseSchema);

export type createHiringSourceDto = z.infer<typeof createHiringSourceSchema>;
export type updateHiringSourceDto = z.infer<typeof updateHiringSourceSchema>;
export type hiringSourceResponseDto = z.infer<
  typeof hiringSourceResponseSchema
>;
export type hiringSourcesResponseDto = z.infer<
  typeof hiringSourcesResponseSchema
>;
