import { describe, expect, it } from 'vitest'
import { availabilitySchema, bookSchema, capturePaypalSchema, contactSchema, paySchema } from './site.schemas'

describe('site.schemas', () => {
  const baseBooking = {
    vehicleId: 'ckvvehicle000000000000001',
    startDate: '2026-07-01T10:00:00.000Z',
    endDate: '2026-07-03T10:00:00.000Z',
    firstName: 'Aya',
    lastName: 'Benali',
    email: 'aya@example.com',
    phone: '+212600000000',
    pickupLocation: 'Casablanca Airport',
    returnLocation: 'Casablanca Airport',
  }

  it('defaults optional public booking arrays and source', () => {
    expect(bookSchema.parse(baseBooking)).toMatchObject({
      source: 'PUBLIC_SITE',
      selectedInsurancePolicyIds: [],
      additionalDrivers: [],
    })
  })

  it('validates nested additional drivers instead of letting anonymous payloads drift into services', () => {
    expect(bookSchema.parse({
      ...baseBooking,
      additionalDrivers: [{ firstName: 'Omar', lastName: 'Alaoui', email: 'omar@example.com', driverLicense: 'DL-2' }],
    }).additionalDrivers).toHaveLength(1)

    expect(() => bookSchema.parse({
      ...baseBooking,
      additionalDrivers: [{ firstName: 'Omar', lastName: 'Alaoui', email: 'not-email', driverLicense: 'DL-2' }],
    })).toThrow()
  })

  it('rejects malformed availability and payment payloads at the schema layer', () => {
    expect(availabilitySchema.parse({
      vehicleId: 'ckvvehicle000000000000001',
      startDate: '2026-07-01T10:00:00.000Z',
      endDate: '2026-07-03T10:00:00.000Z',
    })).toMatchObject({ vehicleId: 'ckvvehicle000000000000001' })

    expect(() => availabilitySchema.parse({ vehicleId: 'not-cuid', startDate: 'x', endDate: 'y' })).toThrow()
    expect(() => paySchema.parse({ provider: 'STRIPE', successUrl: 'https://ok.test', failureUrl: 'https://fail.test' })).toThrow()
    expect(paySchema.parse({ provider: 'PAYPAL', successUrl: 'https://ok.test', failureUrl: 'https://fail.test', accessToken: 'booking-access-token-123' }).currency).toBe('MAD')
  })

  it('keeps contact, PayPal capture, and guest booking essentials strict', () => {
    expect(contactSchema.parse({ name: 'Aya', email: 'aya@example.com', message: 'Hello' })).toEqual({
      name: 'Aya', email: 'aya@example.com', message: 'Hello',
    })
    expect(capturePaypalSchema.parse({ paypalOrderId: 'ORDER-1' })).toEqual({ paypalOrderId: 'ORDER-1' })
    expect(() => bookSchema.parse({ ...baseBooking, email: 'bad-email' })).toThrow()
    expect(() => contactSchema.parse({ name: '', email: 'bad', message: '' })).toThrow()
  })
})
