import { describe, expect, it } from 'vitest';
import { CONTENT_PACK, getContentPack } from '../content/pack';
import { buildChecklist } from './checklist';
import { checklistIcs } from './ics';
import { classify } from './classify';
import type { AISystem, AssessmentAnswers } from './types';

const answers: AssessmentAnswers = {
  isAISystem: true,
  role: 'deployer',
  isGPAIModel: false,
  prohibitedPractices: [],
  annexIIICategories: ['a3-employment'],
  art63Exception: false,
  involvesProfiling: false,
  isAnnexISafetyComponent: false,
  transparencyTriggers: [],
};

const system: AISystem = {
  id: 'sys-1',
  name: 'Bewerber-Screening; GmbH, Test',
  purpose: 'Vorauswahl',
  vendor: '',
  owner: '',
  createdAt: '2026-07-01T00:00:00.000Z',
  updatedAt: '2026-07-01T00:00:00.000Z',
  answers,
  classification: classify(answers, CONTENT_PACK),
  obligationStatus: {},
};

const TODAY = '2026-07-04';
const TIMESTAMP = '2026-07-04T12:00:00.000Z';

function makeIcs(): string {
  const checklist = buildChecklist([system], CONTENT_PACK, ['DE'], TODAY);
  return checklistIcs({
    entries: [...checklist.dueNow, ...checklist.upcoming],
    deadlines: CONTENT_PACK.deadlines,
    today: TODAY,
    timestamp: TIMESTAMP,
    calendarName: 'AI-Act-Kompass',
  });
}

describe('checklistIcs', () => {
  it('erzeugt einen gültigen VCALENDAR-Rahmen mit CRLF', () => {
    const ics = makeIcs();
    expect(ics.startsWith('BEGIN:VCALENDAR\r\n')).toBe(true);
    expect(ics.endsWith('END:VCALENDAR\r\n')).toBe(true);
    expect(ics).toContain('VERSION:2.0');
    expect(ics).toContain('X-WR-CALNAME:AI-Act-Kompass');
    expect(ics.includes('\n') && !ics.replace(/\r\n/g, '').includes('\n')).toBe(true);
  });

  it('exportiert nur zukünftige Fristen als ganztägige Termine mit stabiler UID', () => {
    const ics = makeIcs();
    // Art.-26-Pflichten (2027-12-02) sind enthalten …
    expect(ics).toContain('DTSTART;VALUE=DATE:20271202');
    expect(ics).toContain('DTEND;VALUE=DATE:20271203');
    expect(ics).toContain('UID:sys-1-ob-deployer-oversight@ai-act-kompass');
    // … verstrichene (KI-Kompetenz 2025-02-02) nicht:
    expect(ics).not.toContain('DTSTART;VALUE=DATE:20250202');
    // Allgemeine zukünftige Geltungsfristen sind enthalten:
    expect(ics).toContain('UID:deadline-dl-annex3@ai-act-kompass');
  });

  it('setzt zwei Erinnerungen (30 und 7 Tage vorher) je Termin', () => {
    const ics = makeIcs();
    const eventCount = (ics.match(/BEGIN:VEVENT/g) ?? []).length;
    expect((ics.match(/TRIGGER:-P30D/g) ?? []).length).toBe(eventCount);
    expect((ics.match(/TRIGGER:-P7D/g) ?? []).length).toBe(eventCount);
    expect(eventCount).toBeGreaterThan(3);
  });

  it('maskiert Sonderzeichen und faltet lange Zeilen RFC-konform', () => {
    const ics = makeIcs();
    // Systemname enthält ; und , — müssen maskiert sein:
    expect(ics).toContain('Bewerber-Screening\\;');
    expect(ics.replace(/\r\n /g, '')).toContain('GmbH\\, Test');
    // Keine entfaltete Zeile länger als 75 Oktette:
    for (const line of ics.split('\r\n')) {
      expect(new TextEncoder().encode(line).length).toBeLessThanOrEqual(75);
    }
  });

  it('lokalisiert Termintitel über den Content-Pack', () => {
    const packFr = getContentPack('fr');
    const checklist = buildChecklist(
      [{ ...system, classification: classify(answers, packFr, 'fr') }],
      packFr,
      [],
      TODAY,
    );
    const ics = checklistIcs({
      entries: [...checklist.dueNow, ...checklist.upcoming],
      deadlines: packFr.deadlines,
      today: TODAY,
      timestamp: TIMESTAMP,
      calendarName: 'AI-Act-Kompass',
    });
    expect(ics.replace(/\r\n /g, '')).toContain('contrôle humain');
  });
});
