import { afterEach, describe, expect, it, vi } from 'vitest';
import {
  CAMPUS_ATTENDANCE_PRINT_POPUP_BLOCKED_MESSAGE,
  type CampusAttendancePrintWindow,
  buildCampusAttendancePrintHtml,
  openCampusAttendancePrintReport,
} from '@/business/campus-attendance/printReport';
import {
  CAMPUS_ATTENDANCE_TEST_SEED,
  campusAttendancePrintInputSeed,
  campusAttendanceStatsSeed,
} from '@/test-seeds/campusAttendance';

describe('campus attendance print report', () => {
  afterEach(() => {
    vi.useRealTimers();
  });

  it('builds printable HTML with escaped report metadata and attendance percentages', () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2026-06-08T14:05:00.000Z'));

    const html = buildCampusAttendancePrintHtml(campusAttendancePrintInputSeed);

    expect(html).toContain(`<title>${CAMPUS_ATTENDANCE_TEST_SEED.reportTitleEscaped} - Mon, Jun 8, 2026</title>`);
    expect(html).toContain(`FRAMEworks ${CAMPUS_ATTENDANCE_TEST_SEED.reportTitleEscaped}`);
    expect(html).toContain(
      `Printed by: ${CAMPUS_ATTENDANCE_TEST_SEED.generatedByName} (${CAMPUS_ATTENDANCE_TEST_SEED.generatedByRoleEscaped})`,
    );
    expect(html).toContain(CAMPUS_ATTENDANCE_TEST_SEED.campusFullNameEscaped);
    expect(html).toContain(CAMPUS_ATTENDANCE_TEST_SEED.todayNotesEscaped);
    expect(html).toContain('<div class="value green">90%</div>');
    expect(html).toContain('<div class="value amber">85%</div>');
    expect(html).toContain('<td class="pct-warn">80.0%</td>');
  });

  it('shows explicit no-data labels when the selected print scope has no records', () => {
    const html = buildCampusAttendancePrintHtml({
      ...campusAttendancePrintInputSeed,
      campusesToPrint: [
        {
          ...campusAttendanceStatsSeed,
          todayPct: null,
          weekAvg: null,
          recentData: [],
          todayRecord: null,
        },
      ],
      printTodayRecords: [],
      printWeekRecords: [],
    });

    expect(html).toContain('<div class="value ">No data</div>');
    expect(html).toContain('No attendance data recorded for today.');
    expect(html).toContain('Today: <span class="">N/A</span>');
    expect(html).toContain('Week Avg: <span class="">N/A</span>');
  });

  it('returns an explicit blocked-popup result when the report window cannot open', () => {
    const result = openCampusAttendancePrintReport({
      input: campusAttendancePrintInputSeed,
      openWindow: () => null,
    });

    expect(result).toEqual({ ok: false, reason: 'popup-blocked' });
    expect(CAMPUS_ATTENDANCE_PRINT_POPUP_BLOCKED_MESSAGE).toContain('allow pop-ups');
  });

  it('writes, focuses, and schedules print when the report window opens', () => {
    const write = vi.fn();
    const close = vi.fn();
    const focus = vi.fn();
    const print = vi.fn();
    const schedulePrint = vi.fn((callback: () => void) => callback());
    const printWindow: CampusAttendancePrintWindow = {
      document: {
        write,
        close,
      },
      focus,
      print,
    };

    const result = openCampusAttendancePrintReport({
      input: campusAttendancePrintInputSeed,
      openWindow: () => printWindow,
      schedulePrint,
    });

    expect(result).toEqual({ ok: true });
    expect(write).toHaveBeenCalledWith(expect.stringContaining('FRAMEworks'));
    expect(close).toHaveBeenCalled();
    expect(focus).toHaveBeenCalled();
    expect(schedulePrint).toHaveBeenCalled();
    expect(print).toHaveBeenCalled();
  });
});
