import { z } from "zod"

import { passwordValidator } from "@/utils/zod/passwordValidator"
import { phoneValidator } from "@/utils/zod/phoneValidator"

export const signupErrors = {
  COUNTRY_REQUIRED: "COUNTRY_REQUIRED",
  FIRST_NAME_REQUIRED: "FIRST_NAME_REQUIRED",
  LAST_NAME_REQUIRED: "LAST_NAME_REQUIRED",
  EMAIL_INVALID: "EMAIL_INVALID",
  EMAIL_REQUIRED: "EMAIL_REQUIRED",
  PHONE_REQUIRED: "PHONE_REQUIRED",
  PHONE_REQUESTED: "PHONE_REQUESTED",
  BIRTH_DATE_REQUIRED: "BIRTH_DATE_REQUIRED",
  PASSWORD_REQUIRED: "PASSWORD_REQUIRED",
  TERMS_REQUIRED: "TERMS_REQUIRED",
  ZIP_CODE_REQUIRED: "ZIP_CODE_REQUIRED",
} as const

export const signUpSchema = z.object({
  firstName: z
    .string()
    .max(250)
    .trim()
    .min(1, signupErrors.FIRST_NAME_REQUIRED),
  lastName: z.string().max(250).trim().min(1, signupErrors.LAST_NAME_REQUIRED),
  email: z
    .string()
    .max(250)
    .min(1, signupErrors.EMAIL_REQUIRED)
    .email({ message: signupErrors.EMAIL_INVALID }),
  phoneNumber: phoneValidator(
    signupErrors.PHONE_REQUIRED,
    signupErrors.PHONE_REQUESTED
  ),
  dateOfBirth: z.string().min(1, {
    message: signupErrors.BIRTH_DATE_REQUIRED,
  }),
  address: z.object({
    countryCode: z
      .string({
        required_error: signupErrors.COUNTRY_REQUIRED,
        invalid_type_error: signupErrors.COUNTRY_REQUIRED,
      })
      .min(1, signupErrors.COUNTRY_REQUIRED),
    zipCode: z.string().min(1, signupErrors.ZIP_CODE_REQUIRED),
  }),
  password: passwordValidator(signupErrors.PASSWORD_REQUIRED),
  termsAccepted: z
    .boolean()
    .refine((value) => value === true, signupErrors.TERMS_REQUIRED),
})

export type SignUpSchema = z.infer<typeof signUpSchema>
