import { z } from 'zod'
import { format } from 'date-fns'

export const MAX_ITEMS = 50

export const enrollment = z.object({
  user: z
    .object(
      {
        id: z.string(),
        name: z.string(),
        email: z.string(),
        cpf: z.string('a')
      },
      { error: 'Escolha um colaborador' }
    )
    .required(),
  course: z
    .object(
      {
        id: z.string(),
        name: z.string(),
        access_period: z.number(),
        unit_price: z.number()
      },
      { error: 'Escolha um curso' }
    )
    .required(),
  deduplication_window: z
    .object({
      offset_days: z.number()
    })
    .optional(),
  scheduled_for: z
    .date()
    .optional()
    .transform((date) => (date ? format(date, 'yyyy-MM-dd') : undefined))
})

export const formSchema = z.object({
  enrollments: z.array(enrollment).min(1).max(MAX_ITEMS)
})

export type Schema = z.infer<typeof formSchema>
