import { describe, expect, it } from 'vitest'
import {
  SHEET_HEIGHT,
  SHEET_WIDTH,
  resolvePointPosition,
  resolveVehicleSheetClick,
  severityColors,
  viewBoxes,
  type VehicleSheetPoint,
} from './VehicleConditionSheet'

const rect = { left: 10, top: 20, width: SHEET_WIDTH * 2, height: SHEET_HEIGHT * 2 } as Pick<DOMRect, 'left' | 'top' | 'width' | 'height'>

function sheetClientPoint(x: number, y: number) {
  return {
    clientX: rect.left + (x / SHEET_WIDTH) * rect.width,
    clientY: rect.top + (y / SHEET_HEIGHT) * rect.height,
  }
}

describe('VehicleConditionSheet geometry helpers', () => {
  it('maps percentage positions into absolute sheet coordinates per view', () => {
    expect(resolvePointPosition({ viewType: 'TOP', x: 50, y: 50, severity: 'MINOR' })).toEqual({
      cx: viewBoxes.TOP.x + viewBoxes.TOP.w / 2,
      cy: viewBoxes.TOP.y + viewBoxes.TOP.h / 2,
    })
    expect(resolvePointPosition({ viewType: 'LEFT', x: 0, y: 100, severity: 'MAJOR' })).toEqual({
      cx: viewBoxes.LEFT.x,
      cy: viewBoxes.LEFT.y + viewBoxes.LEFT.h,
    })
  })

  it('defaults missing viewType to TOP for legacy damage points', () => {
    const legacyPoint = { x: 25, y: 10, severity: 'MODERATE' } satisfies VehicleSheetPoint

    expect(resolvePointPosition(legacyPoint)).toEqual({
      cx: viewBoxes.TOP.x + viewBoxes.TOP.w * 0.25,
      cy: viewBoxes.TOP.y + viewBoxes.TOP.h * 0.1,
    })
  })

  it('resolves clicks inside each vehicle region to local percentages', () => {
    const topCenter = sheetClientPoint(viewBoxes.TOP.x + viewBoxes.TOP.w / 2, viewBoxes.TOP.y + viewBoxes.TOP.h / 2)
    const frontCorner = sheetClientPoint(viewBoxes.FRONT.x, viewBoxes.FRONT.y)
    const rearCorner = sheetClientPoint(viewBoxes.REAR.x + viewBoxes.REAR.w, viewBoxes.REAR.y + viewBoxes.REAR.h)

    expect(resolveVehicleSheetClick(topCenter.clientX, topCenter.clientY, rect)).toEqual({ viewType: 'TOP', x: 50, y: 50 })
    expect(resolveVehicleSheetClick(frontCorner.clientX, frontCorner.clientY, rect)).toEqual({ viewType: 'FRONT', x: 0, y: 0 })
    expect(resolveVehicleSheetClick(rearCorner.clientX, rearCorner.clientY, rect)).toEqual({ viewType: 'REAR', x: 100, y: 100 })
  })

  it('rejects clicks outside vehicle regions instead of creating impossible damage points', () => {
    const outside = sheetClientPoint(40, 40)

    expect(resolveVehicleSheetClick(outside.clientX, outside.clientY, rect)).toBeNull()
  })

  it('keeps severity color mapping explicit for rendering and comparison markers', () => {
    expect(severityColors).toEqual({
      MINOR: '#f59e0b',
      MODERATE: '#f97316',
      MAJOR: '#dc2626',
    })
  })
})
