import { z } from 'zod';

// Factory functions to create schemas with translated messages
export const createLoginSchema = (t: (key: string) => string) => z.object({
  email: z.string().email(t('errors.invalidEmail')),
  password: z.string().min(6, t('errors.passwordTooShort')),
});

export const createRegisterSchema = (t: (key: string) => string) => z.object({
  firstName: z.string().min(2, t('errors.firstNameTooShort')),
  lastName: z.string().min(2, t('errors.lastNameTooShort')),
  email: z.string().email(t('errors.invalidEmail')),
  password: z.string().min(6, t('errors.passwordTooShort')),
  confirmPassword: z.string(),
  userType: z.enum(['doctor', 'patient', 'admin', 'staff']).optional(),
  isDentist: z.boolean().optional(),
}).refine((data) => data.password === data.confirmPassword, {
  message: t('errors.passwordMismatch'),
  path: ['confirmPassword'],
}).refine((data) => {
  if (data.userType === 'doctor') {
    return data.isDentist !== undefined;
  }
  return true;
}, {
  message: t('errors.dentistRequired'),
  path: ['isDentist'],
});

export const createForgotPasswordSchema = (t: (key: string) => string) => z.object({
  email: z.string().email(t('errors.invalidEmail')),
});

export const createResetPasswordSchema = (t: (key: string) => string) => z.object({
  token: z.string().min(1, t('errors.resetTokenRequired')),
  newPassword: z.string().min(6, t('errors.passwordTooShort')),
  confirmPassword: z.string(),
}).refine((data) => data.newPassword === data.confirmPassword, {
  message: t('errors.passwordMismatch'),
  path: ['confirmPassword'],
});

// Default schemas for backward compatibility (using English)
export const loginSchema = z.object({
  email: z.string().email('Invalid email address'),
  password: z.string().min(6, 'Password must be at least 6 characters'),
});

export const registerSchema = z.object({
  firstName: z.string().min(2, 'First name must be at least 2 characters'),
  lastName: z.string().min(2, 'Last name must be at least 2 characters'),
  email: z.string().email('Invalid email address'),
  password: z.string().min(6, 'Password must be at least 6 characters'),
  confirmPassword: z.string(),
  userType: z.enum(['doctor', 'patient', 'admin', 'staff']).optional(),
}).refine((data) => data.password === data.confirmPassword, {
  message: 'Passwords do not match',
  path: ['confirmPassword'],
});

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

export const resetPasswordSchema = z.object({
  token: z.string().min(1, 'Reset token is required'),
  newPassword: z.string().min(6, 'Password must be at least 6 characters'),
  confirmPassword: z.string(),
}).refine((data) => data.newPassword === data.confirmPassword, {
  message: 'Passwords do not match',
  path: ['confirmPassword'],
});

export type LoginInput = z.infer<typeof loginSchema>;
export type RegisterInput = z.infer<typeof registerSchema>;
export type ForgotPasswordInput = z.infer<typeof forgotPasswordSchema>;
export type ResetPasswordInput = z.infer<typeof resetPasswordSchema>;
