import { describe, expect, it } from 'vitest'
import {
  availabilityQuerySchema,
  calendarBlockSchema,
  calendarQuerySchema,
  listQuerySchema,
  pricingConfigSchema,
  pricingRuleSchema,
  vehicleSchema,
} from './vehicle.schemas'

describe('vehicle.schemas', () => {
  it('applies safe create defaults without mutating required vehicle identity fields', () => {
    const parsed = vehicleSchema.parse({
      make: 'Toyota',
      model: 'Yaris',
      year: 2026,
      licensePlate: '123-A-45',
      category: 'ECONOMY',
    })

    expect(parsed).toMatchObject({
      color: '',
      seats: 5,
      transmission: 'AUTOMATIC',
      fuelType: 'GASOLINE',
      features: [],
      isPublished: true,
      pickupLocations: [],
      allowDifferentDropoff: false,
      dropoffLocations: [],
    })
  })

  it('rejects impossible vehicle years and invalid fleet capacity inputs', () => {
    expect(() => vehicleSchema.parse({
      make: 'Toyota',
      model: 'Yaris',
      year: 1989,
      licensePlate: '123-A-45',
      category: 'ECONOMY',
    })).toThrow()

    expect(() => vehicleSchema.parse({
      make: 'Ford',
      model: 'Transit',
      year: 2026,
      licensePlate: '456-B-78',
      category: 'VAN',
      seats: 0,
    })).toThrow()
  })

  it('coerces list and calendar query params while enforcing page and month bounds', () => {
    expect(listQuerySchema.parse({ page: '3', pageSize: '50', status: 'AVAILABLE' })).toEqual({
      page: 3,
      pageSize: 50,
      status: 'AVAILABLE',
    })
    expect(calendarQuerySchema.parse({ year: '2026', month: '6' })).toEqual({ year: 2026, month: 6 })
    expect(() => calendarQuerySchema.parse({ year: '2026', month: '13' })).toThrow()
    expect(() => listQuerySchema.parse({ page: '0' })).toThrow()
  })

  it('accepts date-only and offset timestamps for calendar blocks', () => {
    expect(calendarBlockSchema.parse({ startDate: '2026-07-01', endDate: '2026-07-03' })).toMatchObject({
      startDate: '2026-07-01',
      endDate: '2026-07-03',
      type: 'MANUAL',
    })
    expect(calendarBlockSchema.parse({
      startDate: '2026-07-01T10:00:00.000Z',
      endDate: '2026-07-03T10:00:00.000Z',
      type: 'MAINTENANCE',
    }).type).toBe('MAINTENANCE')
  })

  it('protects automatic pricing configuration bounds', () => {
    expect(pricingConfigSchema.parse({
      pricingMode: 'AUTOMATIC',
      baseDailyRate: 350,
      maxDailyPriceMovementPct: 25,
      automaticPricingEnabled: true,
      approvalRequired: true,
    })).toMatchObject({
      pricingMode: 'AUTOMATIC',
      baseDailyRate: 350,
      maxDailyPriceMovementPct: 25,
      automaticPricingEnabled: true,
    })

    expect(() => pricingConfigSchema.parse({
      pricingMode: 'AUTOMATIC',
      baseDailyRate: 350,
      maxDailyPriceMovementPct: 101,
    })).toThrow()
  })

  it('keeps pricing rule adjustments inside expected rate limits', () => {
    expect(pricingRuleSchema.parse({
      name: 'Summer peak',
      startDate: '2026-08-01',
      endDate: '2026-08-31',
      automaticAdjustmentPct: 50,
    })).toMatchObject({
      name: 'Summer peak',
      ruleType: 'DATE_RANGE',
      automaticAdjustmentPct: 50,
      isActive: true,
      sortOrder: 0,
    })

    expect(() => pricingRuleSchema.parse({
      name: 'Broken multiplier',
      startDate: '2026-08-01',
      endDate: '2026-08-31',
      automaticAdjustmentPct: 250,
    })).toThrow()
  })

  it('does not validate date ordering in query-only availability input', () => {
    expect(availabilityQuerySchema.parse({ startDate: '2026-08-10', endDate: '2026-08-01' })).toEqual({
      startDate: '2026-08-10',
      endDate: '2026-08-01',
    })
  })
})
