import { z } from "zod"

import { nullableStringValidator } from "@/utils/zod/stringValidator"

const quantitySchemaWithoutRefine = z.object({
  quantityWithPoints: z.number().nullable(),
  quantityWithCard: z.number().nullable(),
})

export const quantitySchema = z
  .object({})
  .merge(quantitySchemaWithoutRefine)
  .refine(
    (data) =>
      (data.quantityWithPoints ?? 0) > 0 || (data.quantityWithCard ?? 0) > 0,
    {
      message: "You must select at least one quantity",
      path: ["quantityWithCard"],
    }
  )

export const ancillaryFormSchema = z
  .object({
    deliveryTime: z.string(),
    optionalText: z.string(),
    termsAndConditions: z.boolean(),
    paymentMethod: nullableStringValidator,
  })
  .merge(quantitySchemaWithoutRefine)
  .refine(
    (data) =>
      (data.quantityWithPoints ?? 0) > 0 || (data.quantityWithCard ?? 0) > 0,
    {
      message: "You must select at least one quantity",
      path: ["quantityWithCard"],
    }
  )

export type AncillaryQuantityFormData = z.output<
  typeof quantitySchemaWithoutRefine
>
export type AncillaryFormData = z.output<typeof ancillaryFormSchema>
