import { describe, expect, it } from 'vitest'
import { approveLicenseSchema, customerSchema, flagSchema, listQuerySchema } from './customer.schemas'

describe('customer schema contracts', () => {
  it('normalizes create payload identity fields and optional text', () => {
    const parsed = customerSchema.parse({
      firstName: '  Aya  ',
      lastName: '  Haddad  ',
      email: 'AYA@EXAMPLE.TEST',
      phone: '  +212600000000  ',
      notes: '  VIP renter  ',
      licenseNumber: '  B-123456  ',
      dateOfBirth: '1994-05-01T00:00:00.000Z',
    })

    expect(parsed).toMatchObject({
      firstName: 'Aya',
      lastName: 'Haddad',
      email: 'aya@example.test',
      phone: '+212600000000',
      notes: 'VIP renter',
      licenseNumber: 'B-123456',
    })
  })

  it('coerces list pagination and preserves supported filters', () => {
    expect(listQuerySchema.parse({ page: '3', pageSize: '40', q: 'aya', flagged: 'true' })).toEqual({
      page: 3,
      pageSize: 40,
      q: 'aya',
      flagged: 'true',
    })
  })

  it('rejects impossible pagination and unsafe customer field lengths', () => {
    expect(() => listQuerySchema.parse({ page: '0' })).toThrow()
    expect(() => customerSchema.parse({ firstName: '', lastName: 'Haddad', email: 'aya@example.test' })).toThrow()
    expect(() => customerSchema.parse({ firstName: 'Aya', lastName: 'Haddad', email: 'not-email' })).toThrow()
    expect(() => customerSchema.parse({ firstName: 'Aya', lastName: 'Haddad', email: 'aya@example.test', notes: 'x'.repeat(2001) })).toThrow()
  })

  it('limits license approvals to explicit approve or deny decisions', () => {
    expect(approveLicenseSchema.parse({ decision: 'APPROVE', note: 'Verified manually' })).toEqual({
      decision: 'APPROVE',
      note: 'Verified manually',
    })
    expect(() => approveLicenseSchema.parse({ decision: 'MAYBE' })).toThrow()
  })

  it('keeps customer flagging reason optional', () => {
    expect(flagSchema.parse({})).toEqual({})
    expect(flagSchema.parse({ reason: 'Chargeback risk' })).toEqual({ reason: 'Chargeback risk' })
  })
})
