import { describe, expect, it, vi } from 'vitest'
import {
  buildConfigForm,
  buildRuleForm,
  centsToInput,
  inputToCents,
  numericInputToInt,
  toDateInput,
  type PricingConfiguration,
  type PricingRule,
} from './VehiclePricingManager'

const configuration: PricingConfiguration = {
  id: 'cfg_1',
  vehicleId: 'veh_1',
  pricingMode: 'AUTOMATIC',
  baseDailyRate: 50000,
  weeklyRate: 280000,
  weekendRate: 65000,
  holidayRate: null,
  monthlyRate: 950000,
  longTermDailyRate: null,
  minimumDailyRate: 40000,
  maximumDailyRate: 80000,
  maxDailyPriceMovementPct: 12,
  automaticPricingEnabled: true,
  approvalRequired: false,
  createdAt: '2026-06-01T00:00:00.000Z',
  updatedAt: '2026-06-02T00:00:00.000Z',
}

const rule: PricingRule = {
  id: 'rule_1',
  name: 'Summer',
  ruleType: 'SEASONAL',
  startDate: '2026-07-01T00:00:00.000Z',
  endDate: '2026-08-31T00:00:00.000Z',
  dailyRate: 70000,
  weeklyRate: null,
  weekendRate: 90000,
  monthlyRate: null,
  minDailyRate: 60000,
  maxDailyRate: 110000,
  automaticAdjustmentPct: 15,
  priceAdjustment: 5000,
  isActive: false,
  sortOrder: 4,
  createdAt: '2026-06-01T00:00:00.000Z',
  updatedAt: '2026-06-02T00:00:00.000Z',
}

describe('VehiclePricingManager helpers', () => {
  it('converts cents to display input strings and empty nullable values', () => {
    expect(centsToInput(123456)).toBe('1234.56')
    expect(centsToInput(0)).toBe('0.00')
    expect(centsToInput(null)).toBe('')
    expect(centsToInput(undefined)).toBe('')
  })

  it('converts currency inputs to cents while rejecting blank or invalid values', () => {
    expect(inputToCents('499.99')).toBe(49999)
    expect(inputToCents(' 50 ')).toBe(5000)
    expect(inputToCents('')).toBeNull()
    expect(inputToCents('abc')).toBeNull()
  })

  it('parses integer-only fields without accepting blank values', () => {
    expect(numericInputToInt('12')).toBe(12)
    expect(numericInputToInt('12.8')).toBe(12)
    expect(numericInputToInt('')).toBeNull()
    expect(numericInputToInt('nope')).toBeNull()
  })

  it('builds a stable editable configuration form from API pricing data', () => {
    expect(buildConfigForm(configuration)).toEqual({
      pricingMode: 'AUTOMATIC',
      baseDailyRate: '500.00',
      weeklyRate: '2800.00',
      weekendRate: '650.00',
      holidayRate: '',
      monthlyRate: '9500.00',
      longTermDailyRate: '',
      minimumDailyRate: '400.00',
      maximumDailyRate: '800.00',
      maxDailyPriceMovementPct: '12',
      automaticPricingEnabled: true,
      approvalRequired: false,
    })
  })

  it('builds an editable rule form from an existing pricing rule', () => {
    expect(buildRuleForm(rule)).toEqual({
      id: 'rule_1',
      name: 'Summer',
      ruleType: 'SEASONAL',
      startDate: '2026-07-01',
      endDate: '2026-08-31',
      dailyRate: '700.00',
      weeklyRate: '',
      weekendRate: '900.00',
      monthlyRate: '',
      minDailyRate: '600.00',
      maxDailyRate: '1100.00',
      automaticAdjustmentPct: '15',
      priceAdjustment: '50.00',
      isActive: false,
      sortOrder: '4',
    })
  })

  it('defaults new rules to today through six days later', () => {
    vi.setSystemTime(new Date('2026-06-09T12:00:00.000Z'))

    expect(buildRuleForm()).toMatchObject({
      name: '',
      ruleType: 'DATE_RANGE',
      startDate: '2026-06-09',
      endDate: '2026-06-15',
      isActive: true,
      sortOrder: '0',
    })

    vi.useRealTimers()
  })

  it('trims ISO timestamps for date input controls', () => {
    expect(toDateInput('2026-10-04T22:15:00.000Z')).toBe('2026-10-04')
  })
})
