import { describe, expect, it } from 'vitest';
import { DayPeriod } from '@prisma/client';
import { leaveDaysBetween, normalizeDayPeriod } from './leave';

describe('leave day math', () => {
  it('counts inclusive calendar days for full ranges', () => {
    expect(leaveDaysBetween('2026-11-01', '2026-11-01', 'full')).toBe(1);
    expect(leaveDaysBetween('2026-11-01', '2026-11-02', 'full')).toBe(2);
    expect(leaveDaysBetween('2026-11-01', '2026-11-03', 'full')).toBe(3);
  });

  it('allows half-day only on single-day ranges', () => {
    expect(leaveDaysBetween('2026-11-01', '2026-11-01', 'morning')).toBe(0.5);
    expect(leaveDaysBetween('2026-11-01', '2026-11-01', 'afternoon')).toBe(0.5);
    // multi-day still returns calendar span; API assertCanApply rejects half-day period
    expect(leaveDaysBetween('2026-11-01', '2026-11-02', 'morning')).toBe(2);
  });

  it('normalizeDayPeriod forces full across multi-day', () => {
    expect(normalizeDayPeriod('2026-11-01', '2026-11-02', 'morning')).toBe(
      DayPeriod.full
    );
    expect(normalizeDayPeriod('2026-11-01', '2026-11-01', 'afternoon')).toBe(
      DayPeriod.afternoon
    );
  });

  it('rejects inverted ranges', () => {
    expect(leaveDaysBetween('2026-11-05', '2026-11-01', 'full')).toBe(0);
  });
});
