import { describe, it, expect } from 'vitest';
import { DosageFrequency } from '../enums/regimen.enums.js';
import { getFrequencyMultiplier, calculateQuantityNeeded } from './frequency.js';

describe('getFrequencyMultiplier', () => {
  it.each([
    [DosageFrequency.DAILY, undefined, 1],
    [DosageFrequency.TWICE_DAILY, undefined, 2],
    [DosageFrequency.THREE_TIMES_DAILY, undefined, 3],
    [DosageFrequency.WEEKLY, undefined, 1 / 7],
    [DosageFrequency.EVERY_OTHER_DAY, undefined, 1 / 2],
    [DosageFrequency.AS_NEEDED, undefined, 0],
    [DosageFrequency.CUSTOM, 4, 4],
    [DosageFrequency.CUSTOM, undefined, 0],
  ])('%s (customPerDay=%s) = %s', (frequency, custom, expected) => {
    expect(getFrequencyMultiplier(frequency, custom)).toBe(expected);
  });
});

describe('calculateQuantityNeeded', () => {
  it('daily: 2 pills * 7 days = 14', () => {
    expect(calculateQuantityNeeded(2, DosageFrequency.DAILY, 7)).toBe(14);
  });

  it('twice_daily: 1 pill * 2 * 7 days = 14', () => {
    expect(calculateQuantityNeeded(1, DosageFrequency.TWICE_DAILY, 7)).toBe(14);
  });

  it('three_times_daily: 1 pill * 3 * 7 days = 21', () => {
    expect(calculateQuantityNeeded(1, DosageFrequency.THREE_TIMES_DAILY, 7)).toBe(21);
  });

  it('weekly: 1 pill * ceil(7/7) = 1', () => {
    expect(calculateQuantityNeeded(1, DosageFrequency.WEEKLY, 7)).toBe(1);
  });

  it('weekly: 2 pills * ceil(10/7) = 4', () => {
    expect(calculateQuantityNeeded(2, DosageFrequency.WEEKLY, 10)).toBe(4);
  });

  it('every_other_day: 1 pill * ceil(7/2) = 4', () => {
    expect(calculateQuantityNeeded(1, DosageFrequency.EVERY_OTHER_DAY, 7)).toBe(4);
  });

  it('every_other_day: 1 pill * ceil(6/2) = 3', () => {
    expect(calculateQuantityNeeded(1, DosageFrequency.EVERY_OTHER_DAY, 6)).toBe(3);
  });

  it('as_needed: always 0', () => {
    expect(calculateQuantityNeeded(5, DosageFrequency.AS_NEEDED, 30)).toBe(0);
  });

  it('custom: 1 pill * 4/day * 7 days = 28', () => {
    expect(calculateQuantityNeeded(1, DosageFrequency.CUSTOM, 7, 4)).toBe(28);
  });

  it('custom without customFrequencyPerDay: 0', () => {
    expect(calculateQuantityNeeded(1, DosageFrequency.CUSTOM, 7)).toBe(0);
  });

  it('handles fractional dosage: 0.5 pill * 7 days = 3.5', () => {
    expect(calculateQuantityNeeded(0.5, DosageFrequency.DAILY, 7)).toBe(3.5);
  });

  it('single day daily: 1 pill', () => {
    expect(calculateQuantityNeeded(1, DosageFrequency.DAILY, 1)).toBe(1);
  });
});
