import { z } from "zod"

export {
  type AdditionalInfoFormSchema,
  additionalInfoFormSchema,
  findMyBookingErrors,
  type FindMyBookingFormSchema,
  findMyBookingFormSchema,
}

const findMyBookingErrors = {
  BOOKING_NUMBER_INVALID: "BOOKING_NUMBER_INVALID",
  BOOKING_NUMBER_REQUIRED: "BOOKING_NUMBER_REQUIRED",
  FIRST_NAME_REQUIRED: "FIRST_NAME_REQUIRED",
  LAST_NAME_REQUIRED: "LAST_NAME_REQUIRED",
  EMAIL_REQUIRED: "EMAIL_REQUIRED",
} as const

const additionalInfoFormSchema = z.object({
  firstName: z
    .string()
    .trim()
    .max(250)
    .min(1, findMyBookingErrors.FIRST_NAME_REQUIRED),
  email: z.string().max(250).email(findMyBookingErrors.EMAIL_REQUIRED),
})

const findMyBookingFormSchema = additionalInfoFormSchema.extend({
  confirmationNumber: z
    .string()
    .trim()
    .min(1, findMyBookingErrors.BOOKING_NUMBER_REQUIRED)
    .regex(/^[0-9]+(-[0-9])?$/, findMyBookingErrors.BOOKING_NUMBER_INVALID),
  lastName: z
    .string()
    .trim()
    .max(250)
    .min(1, findMyBookingErrors.LAST_NAME_REQUIRED),
})

type AdditionalInfoFormSchema = z.output<typeof additionalInfoFormSchema>

type FindMyBookingFormSchema = z.output<typeof findMyBookingFormSchema>
