/**
 * P1-10 — scheduled report scheduler decision function.
 *
 * Pure: given a ReportPreference row and `now`, returns whether the
 * scheduler should send a report on this tick. The worker calls this
 * once per enabled row per tick; only true results trigger the
 * (more expensive) email build + send.
 */
import { shouldSendReportNow, intervalMsFor } from '../report-scheduler';

const FREQ = {
    weekly: 7 * 24 * 60 * 60 * 1000,
    monthly: 30 * 24 * 60 * 60 * 1000,
    quarterly: 90 * 24 * 60 * 60 * 1000,
};

describe('intervalMsFor (P1-10)', () => {
    it('maps weekly to 7 days', () => {
        expect(intervalMsFor('weekly')).toBe(FREQ.weekly);
    });
    it('maps monthly to 30 days', () => {
        expect(intervalMsFor('monthly')).toBe(FREQ.monthly);
    });
    it('maps quarterly to 90 days', () => {
        expect(intervalMsFor('quarterly')).toBe(FREQ.quarterly);
    });
    it('returns null for unknown frequency (defensive: skip rather than crash)', () => {
        expect(intervalMsFor('hourly')).toBeNull();
        expect(intervalMsFor('')).toBeNull();
    });
});

describe('shouldSendReportNow (P1-10)', () => {
    const NOW = new Date('2026-06-01T12:00:00Z');

    it('returns true when enabled and lastSentAt is null (never sent)', () => {
        expect(shouldSendReportNow({ enabled: true, frequency: 'weekly', lastSentAt: null }, NOW)).toBe(true);
    });

    it('returns false when disabled, regardless of when it last ran', () => {
        expect(shouldSendReportNow({ enabled: false, frequency: 'weekly', lastSentAt: null }, NOW)).toBe(false);
        const long_ago = new Date('2020-01-01T00:00:00Z');
        expect(shouldSendReportNow({ enabled: false, frequency: 'weekly', lastSentAt: long_ago }, NOW)).toBe(false);
    });

    it('returns false when the interval has not elapsed yet (weekly, 6 days ago)', () => {
        const six_days_ago = new Date(NOW.getTime() - 6 * 24 * 60 * 60 * 1000);
        expect(shouldSendReportNow({ enabled: true, frequency: 'weekly', lastSentAt: six_days_ago }, NOW)).toBe(false);
    });

    it('returns true when the interval has elapsed (weekly, 8 days ago)', () => {
        const eight_days_ago = new Date(NOW.getTime() - 8 * 24 * 60 * 60 * 1000);
        expect(shouldSendReportNow({ enabled: true, frequency: 'weekly', lastSentAt: eight_days_ago }, NOW)).toBe(true);
    });

    it('returns false when the interval has not elapsed yet (monthly, 29 days ago)', () => {
        const days_29 = new Date(NOW.getTime() - 29 * 24 * 60 * 60 * 1000);
        expect(shouldSendReportNow({ enabled: true, frequency: 'monthly', lastSentAt: days_29 }, NOW)).toBe(false);
    });

    it('returns true when the interval has elapsed (monthly, 31 days ago)', () => {
        const days_31 = new Date(NOW.getTime() - 31 * 24 * 60 * 60 * 1000);
        expect(shouldSendReportNow({ enabled: true, frequency: 'monthly', lastSentAt: days_31 }, NOW)).toBe(true);
    });

    it('treats a future lastSentAt as "ran already" (clock-skew defence)', () => {
        const future = new Date(NOW.getTime() + 24 * 60 * 60 * 1000);
        expect(shouldSendReportNow({ enabled: true, frequency: 'weekly', lastSentAt: future }, NOW)).toBe(false);
    });

    it('returns false for an unknown frequency (defensive)', () => {
        expect(shouldSendReportNow({ enabled: true, frequency: 'hourly', lastSentAt: null }, NOW)).toBe(false);
    });
});
