import { describe, it, expect, afterEach, vi } from 'vitest';
import {
    AR_TZ,
    formatInstant,
    formatInstantOrDash,
    formatCivilDate,
    formatCivilDateRange,
    todayArgentina,
    parseCivilDate,
    prevCivilDate,
} from '@/lib/dateFormat';

describe('dateFormat.ts', () => {
    describe('AR_TZ constant', () => {
        it('is America/Argentina/Buenos_Aires', () => {
            expect(AR_TZ).toBe('America/Argentina/Buenos_Aires');
        });
    });

    describe('formatInstant (Cat1 — UTC → AR display)', () => {
        it('converts 01:30 UTC to 22:30 ART previous day', () => {
            const iso = '2026-05-01T01:30:00.000Z';
            const result = formatInstant(iso);
            // format es-AR short + medium: "30/4/2026, 22:30:00" o similar
            expect(result).toMatch(/30\/0?4\/2026/);
            expect(result).toMatch(/22:30/);
        });

        it('uses AR timezone regardless of browser TZ', () => {
            const iso = '2026-05-01T12:00:00.000Z'; // 09:00 ART
            const result = formatInstant(iso);
            expect(result).toMatch(/01\/0?5\/2026/);
            expect(result).toMatch(/09:00/);
        });
    });

    describe('formatInstantOrDash (Cat1 nullable)', () => {
        it('returns "—" for null', () => {
            expect(formatInstantOrDash(null)).toBe('—');
        });

        it('returns "—" for undefined', () => {
            expect(formatInstantOrDash(undefined)).toBe('—');
        });

        it('delegates to formatInstant for valid ISO', () => {
            const iso = '2026-05-01T01:30:00.000Z';
            expect(formatInstantOrDash(iso)).toBe(formatInstant(iso));
        });
    });

    describe('formatCivilDate (Cat2 — yyyy-MM-dd → dd/MM/yyyy)', () => {
        it('splits manually without new Date()', () => {
            expect(formatCivilDate('2026-05-01')).toBe('01/05/2026');
        });

        it('preserves day for early-year dates', () => {
            expect(formatCivilDate('2026-01-01')).toBe('01/01/2026');
        });

        it('preserves day for end-of-year dates', () => {
            expect(formatCivilDate('2026-12-31')).toBe('31/12/2026');
        });
    });

    describe('formatCivilDateRange', () => {
        it('renders full range with arrow', () => {
            expect(formatCivilDateRange('2026-05-01', '2026-12-31'))
                .toBe('01/05/2026 → 31/12/2026');
        });

        it('renders open range when hasta is null', () => {
            expect(formatCivilDateRange('2026-05-01', null))
                .toBe('desde 01/05/2026');
        });
    });

    describe('todayArgentina (fix BUG-FE-03)', () => {
        afterEach(() => {
            vi.useRealTimers();
        });

        it('returns 2026-04-30 at 22:30 ART of 30/04 (BUG-FE-03 regression)', () => {
            // 22:30 ART = 01:30 UTC del día siguiente
            vi.useFakeTimers();
            vi.setSystemTime(new Date('2026-05-01T01:30:00.000Z'));

            expect(todayArgentina()).toBe('2026-04-30');
        });

        it('returns 2026-05-01 at 00:30 ART of 01/05', () => {
            // 00:30 ART = 03:30 UTC
            vi.useFakeTimers();
            vi.setSystemTime(new Date('2026-05-01T03:30:00.000Z'));

            expect(todayArgentina()).toBe('2026-05-01');
        });

        it('returns 2024-02-28 at 22:30 ART of 28/02/2024 (bisiesto)', () => {
            // 22:30 ART del 28/02/2024 = 01:30 UTC del 29/02/2024
            vi.useFakeTimers();
            vi.setSystemTime(new Date('2024-02-29T01:30:00.000Z'));

            expect(todayArgentina()).toBe('2024-02-28');
        });

        it('returns 2026-12-31 at 22:30 ART of 31/12 (end of year)', () => {
            // 22:30 ART del 31/12/2026 = 01:30 UTC del 01/01/2027
            vi.useFakeTimers();
            vi.setSystemTime(new Date('2027-01-01T01:30:00.000Z'));

            expect(todayArgentina()).toBe('2026-12-31');
        });
    });

    describe('parseCivilDate', () => {
        it('splits "2026-05-01" into {year, month, day}', () => {
            expect(parseCivilDate('2026-05-01')).toEqual({ year: 2026, month: 5, day: 1 });
        });

        it('does not use new Date() (no UTC creep)', () => {
            const result = parseCivilDate('2026-01-01');
            expect(result.year).toBe(2026);
            expect(result.month).toBe(1);
            expect(result.day).toBe(1);
        });
    });

    describe('prevCivilDate (BUG-FE-04 — fecha cierre NuevaVigencia)', () => {
        it('returns day before (2026-05-01 → 2026-04-30)', () => {
            expect(prevCivilDate('2026-05-01')).toBe('2026-04-30');
        });

        it('crosses month boundary (2026-05-31 → 2026-05-30)', () => {
            expect(prevCivilDate('2026-06-01')).toBe('2026-05-31');
        });

        it('crosses year boundary (2026-01-01 → 2025-12-31)', () => {
            expect(prevCivilDate('2026-01-01')).toBe('2025-12-31');
        });

        it('handles leap year (2024-03-01 → 2024-02-29)', () => {
            expect(prevCivilDate('2024-03-01')).toBe('2024-02-29');
        });
    });
});
