import z from 'zod';
import { IFile } from '../../config/types/interface';
import { DEFAULT_VALUES } from '../../constant/shared';

export const imageSchema = z
  .any()
  .transform((val: IFile) => {
    if (!val || !val?.file_name || val?.file_name?.includes('uploads/common'))
      // return {
      //   file_name: `${process.env.SERVER_HOST}/${process.env.DEFAULT_PROFILE_URL}`,
      //   original_name: 'profile.webp',
      //   file_size: 8,
      //   mime_type: 'image/webp',
      // };
      return null;
    const isHostIncluded = val?.file_name?.includes(process.env.SERVER_HOST);

    return {
      ...val,
      file_name: isHostIncluded
        ? val?.file_name
        : `${process.env.SERVER_HOST}/${val?.file_name}`,
    };
  })
  .optional();

export const personSchema = z
  .object({
    id: z.string(),
    name: z.string().nullable().optional(),
    username: z.string().nullable().optional(),
    image: imageSchema,
  })
  .nullable()
  .optional();

export const defaultTransformSchema = z
  .string()
  .transform((val) => {
    if (!val || val === DEFAULT_VALUES.STRING) return '';
    return val;
  })
  .optional();

export const nullableDefaultTransformSchema = z.preprocess(
  (value) => (value === null || value === undefined ? '' : value),
  z.string().transform((val) => {
    if (!val || val === DEFAULT_VALUES.STRING) return '';
    return val;
  }),
);

export const resendOtpSchema = z.object({
  email: z.email('Invalid email address'),
});

export const verifyOtpSchema = z.object({
  email: z.email('Invalid email address'),
  otp: z.string('OTP is required'),
});

export const resetOtpSchema = z.object({
  email: z.email('Invalid email address'),
  otp: z.string('OTP is required'),
});

export const resetPasswordSchema = z.object({
  email: z.email('Invalid email address'),
  password: z
    .string()
    .min(6, { message: 'Password must be at least 6 characters long' })
    .regex(/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*#?&]{6,}$/, {
      message: 'Password must contain at least one letter and one number',
    }),
});

export const defaultPaginationSchema = z.object({
  search: z.string().trim().optional(),
  department: z.string().trim().optional(),
  page: z.preprocess((value) => {
    if (value === undefined || value === null || value === '') return 1;
    const parsed = Number(value);
    return Number.isNaN(parsed) ? value : parsed;
  }, z.number().int().min(1).default(1)),
  limit: z.preprocess((value) => {
    if (value === undefined || value === null || value === '') return 10;
    const parsed = Number(value);
    return Number.isNaN(parsed) ? value : parsed;
  }, z.number().int().min(1).max(100).default(10)),
});

export const changePasswordSchema = z.object({
  current_password: z.string({ message: 'Current password is required' }),
  new_password: z
    .string()
    .min(6, { message: 'New password must be at least 6 characters long' })
    .regex(/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d@$!%*#?&]{6,}$/, {
      message: 'New password must contain at least one letter and one number',
    }),
});

export const dateTransformSchema = z
  .any()
  .transform((val) => {
    if (val === null || val === undefined) return null;
    const date = val instanceof Date ? val : new Date(val);
    return isNaN(date.getTime()) ? null : date.toISOString();
  })
  .optional()
  .nullable();

export const optionalDate = z.preprocess((value) => {
  if (value === null || value === undefined || value === '') return undefined;
  return value;
}, z.coerce.date().optional());

export type PersonDto = z.infer<typeof personSchema>;
export type ImageDto = z.infer<typeof imageSchema>;
export type resendOtpDto = z.infer<typeof resendOtpSchema>;
export type verifyOtpDto = z.infer<typeof verifyOtpSchema>;
export type resetOtpDto = z.infer<typeof resetOtpSchema>;
export type resetPasswordDto = z.infer<typeof resetPasswordSchema>;
export type changePasswordDto = z.infer<typeof changePasswordSchema>;
export type defaultPaginationDto = z.infer<typeof defaultPaginationSchema>;
