import { describe, it, expect } from 'vitest'
import {
  filterMedicationsForWindow,
  formatMedicationTooltip,
  getMedicationsNearTimestamp,
  xPixelForMedicationTime,
} from '@/composables/chartMedications'

const observations = [
  { observationCode: 'HEART_RATE', value: 72, recordedAt: '2026-06-19T11:00:00Z' },
  { observationCode: 'HEART_RATE', value: 80, recordedAt: '2026-06-19T13:00:00Z' },
  { observationCode: 'RESP_RATE', value: 18, recordedAt: '2026-06-19T12:00:00Z' },
]

const medications = [
  {
    id: '1',
    drugName: 'Metoprolol',
    dose: 5,
    doseUnit: 'mg',
    route: 'IV',
    administeredAt: '2026-06-19T12:30:00Z',
  },
  {
    id: '2',
    drugName: 'Saline',
    dose: 500,
    doseUnit: 'mL',
    route: 'IV',
    administeredAt: '2026-06-19T10:00:00Z',
  },
]

describe('chartMedications', () => {
  it('filtersMedicationsToObservationWindow', () => {
    const result = filterMedicationsForWindow(observations, 'HEART_RATE', medications)
    expect(result).toHaveLength(1)
    expect(result[0].drugName).toBe('Metoprolol')
  })

  it('formatsMedicationTooltip', () => {
    expect(formatMedicationTooltip(medications[0])).toContain('Metoprolol 5mg (IV)')
  })

  it('findsMedicationsNearTimestamp', () => {
    const target = new Date('2026-06-19T12:35:00Z').getTime()
    const near = getMedicationsNearTimestamp(medications, target, 10 * 60 * 1000)
    expect(near).toHaveLength(1)
  })

  it('mapsMedicationTimeToChartPixel', () => {
    const chartArea = { left: 10, right: 110, top: 0, bottom: 100, width: 100, height: 100 }
    const timestamps = ['2026-06-19T11:00:00Z', '2026-06-19T13:00:00Z']
    const x = xPixelForMedicationTime('2026-06-19T12:00:00Z', timestamps, chartArea)
    expect(x).toBe(60)
  })
})
