import { describe, expect, it } from 'vitest'
import {
  buildInspectionSavePayload,
  comparisonPointsForCheckout,
  editableDamagePointsForInspection,
  pointKey,
  type DamageInspection,
  type DamagePoint,
} from './DamageInspectionCard'

function point(overrides: Partial<DamagePoint> = {}): DamagePoint {
  return {
    viewType: 'TOP',
    x: 10,
    y: 20,
    damageType: 'SCRATCH',
    severity: 'MINOR',
    description: null,
    isPreExisting: false,
    ...overrides,
  }
}

function inspection(overrides: Partial<DamageInspection> = {}): DamageInspection {
  return {
    id: 'inspection-1',
    type: 'CHECKOUT',
    mileage: 45000,
    fuelLevel: 'FULL',
    fuelCharge: null,
    generalCondition: 'Clean',
    employeeNotes: 'No issues',
    customerAgreed: true,
    damagePoints: [],
    ...overrides,
  }
}

describe('DamageInspectionCard helpers', () => {
  it('builds stable duplicate keys using rounded coordinates and damage metadata', () => {
    expect(pointKey(point({ x: 12.3456, y: 78.994 }))).toBe('TOP:12.35:78.99:SCRATCH:MINOR')
    expect(pointKey(point({ viewType: 'LEFT', damageType: 'DENT', severity: 'MAJOR' }))).toBe('LEFT:10.00:20.00:DENT:MAJOR')
  })

  it('deduplicates checkout comparison points against stored pre-existing points', () => {
    const duplicate = point({ id: 'cmp-1', isPreExisting: true, description: 'old scratch' })
    const storedDuplicate = point({ id: 'stored-1', isPreExisting: true, description: 'stored copy' })
    const storedUnique = point({ id: 'stored-2', viewType: 'REAR', x: 33, y: 44, isPreExisting: true })

    const result = comparisonPointsForCheckout(
      inspection({ damagePoints: [storedDuplicate, storedUnique] }),
      [duplicate]
    )

    expect(result).toEqual([duplicate, storedUnique])
  })

  it('keeps checkout editing focused on new damage while check-in exposes all points', () => {
    const oldDamage = point({ id: 'old', isPreExisting: true })
    const newDamage = point({ id: 'new', isPreExisting: false })
    const record = inspection({ damagePoints: [oldDamage, newDamage] })

    expect(editableDamagePointsForInspection('CHECKOUT', record)).toEqual([newDamage])
    expect(editableDamagePointsForInspection('CHECKIN', record)).toEqual([oldDamage, newDamage])
  })

  it('builds compact save payloads and omits nullable/empty fields', () => {
    const payload = buildInspectionSavePayload(
      inspection({
        mileage: null,
        fuelCharge: null,
        generalCondition: '',
        employeeNotes: '',
        customerAgreed: false,
        damagePoints: [point({ description: '' })],
      }),
      [point({ id: 'pre', viewType: 'FRONT', description: 'pre-existing chip', isPreExisting: true })]
    )

    expect(payload).toEqual({
      fuelLevel: 'FULL',
      customerAgreed: false,
      damagePoints: [
        {
          viewType: 'FRONT',
          x: 10,
          y: 20,
          damageType: 'SCRATCH',
          severity: 'MINOR',
          description: 'pre-existing chip',
          isPreExisting: true,
        },
        {
          viewType: 'TOP',
          x: 10,
          y: 20,
          damageType: 'SCRATCH',
          severity: 'MINOR',
          isPreExisting: false,
        },
      ],
    })
  })
})
