import vine from '@vinejs/vine'

export const createReservationValidator = vine.compile(
  vine.object({
    roomId: vine.number().exists(async (db, value) => {
      const room = await db.from('rooms').where('id', value).first()
      return !!room
    }),
    customerName: vine.string().trim(),
    customerPhone: vine.string().trim(),
    guestsCount: vine.number().positive(),
    startsAt: vine.string().trim(),
    endsAt: vine.string().trim(),
    notes: vine.string().trim().optional(),
  })
)

export const updateReservationValidator = vine.compile(
  vine.object({
    id: vine.number(),
    roomId: vine
      .number()
      .exists(async (db, value) => {
        const room = await db.from('rooms').where('id', value).first()
        return !!room
      })
      .optional(),
    customerName: vine.string().trim().optional(),
    customerPhone: vine.string().trim().optional(),
    guestsCount: vine.number().positive().optional(),
    startsAt: vine.string().trim().optional(),
    endsAt: vine.string().trim().optional(),
    status: vine.enum(['confirmed', 'cancelled']).optional(),
    notes: vine.string().trim().optional(),
  })
)

export const createRoomValidator = vine.compile(
  vine.object({
    name: vine.string().trim(),
    capacity: vine.number().positive(),
    description: vine.string().trim().optional(),
  })
)

export const updateRoomValidator = vine.compile(
  vine.object({
    id: vine.number(),
    name: vine.string().trim().optional(),
    capacity: vine.number().positive().optional(),
    description: vine.string().trim().optional(),
  })
)
