import { jalaliEngine, gregorianEngine, addDaysIso } from './calendarEngine';

describe('jalaliEngine', () => {
  it('converts a known Gregorian ISO date to Jalaali', () => {
    // Reference pair documented by jalaali-js itself.
    expect(jalaliEngine.fromIso('2016-04-11')).toEqual({ year: 1395, month: 1, day: 23 });
  });

  it('round-trips Jalaali -> ISO -> Jalaali', () => {
    const iso = jalaliEngine.toIso(1404, 5, 15);
    expect(jalaliEngine.fromIso(iso)).toEqual({ year: 1404, month: 5, day: 15 });
  });

  it('knows Esfand length across a leap and a non-leap year', () => {
    expect(jalaliEngine.daysInMonth(1394, 12)).toBe(29);
    expect(jalaliEngine.daysInMonth(1395, 12)).toBe(30);
  });

  it('adds months across a year boundary', () => {
    expect(jalaliEngine.addMonths({ year: 1404, month: 11 }, 2)).toEqual({ year: 1405, month: 1 });
    expect(jalaliEngine.addMonths({ year: 1404, month: 2 }, -3)).toEqual({ year: 1403, month: 11 });
  });
});

describe('gregorianEngine', () => {
  it('is the identity conversion', () => {
    expect(gregorianEngine.fromIso('2026-07-17')).toEqual({ year: 2026, month: 7, day: 17 });
    expect(gregorianEngine.toIso(2026, 7, 17)).toBe('2026-07-17');
  });

  it('knows February length across a leap and a non-leap year', () => {
    expect(gregorianEngine.daysInMonth(2023, 2)).toBe(28);
    expect(gregorianEngine.daysInMonth(2024, 2)).toBe(29);
  });
});

describe('addDaysIso', () => {
  it('adds days across a month boundary', () => {
    expect(addDaysIso('2026-07-30', 3)).toBe('2026-08-02');
  });

  it('subtracts days across a year boundary', () => {
    expect(addDaysIso('2026-01-01', -1)).toBe('2025-12-31');
  });
});
