import { describe, expect, it } from 'vitest';
import { CONTENT_PACK } from '../content/pack';
import { classify } from './classify';
import { actionPlan, annexIVSkeleton, inventoryReport, riskReport } from './documents';
import { deriveObligations } from './obligations';
import type { AISystem, AssessmentAnswers, CompanyProfile } from './types';

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

const system: AISystem = {
  id: 'sys-1',
  name: 'HR-Screening',
  purpose: 'Vorauswahl von Bewerbungen',
  vendor: 'Beispiel GmbH',
  owner: 'Jane Doe',
  createdAt: '2026-07-01T00:00:00.000Z',
  updatedAt: '2026-07-01T00:00:00.000Z',
  answers,
  classification: classify(answers, CONTENT_PACK),
  obligationStatus: { 'ob-risk-mgmt': 'in-progress' },
};

const TODAY = '2026-07-04';

describe('documents', () => {
  it('riskReport enthält Klassifizierung, Begründung und Disclaimer', () => {
    const md = riskReport(system, CONTENT_PACK, TODAY);
    expect(md).toContain('# Risikoklassifizierung');
    expect(md).toContain('Hochrisiko');
    expect(md).toContain('Annex III');
    expect(md).toContain('keine Rechtsberatung');
    expect(md).toContain(CONTENT_PACK.version);
  });

  it('actionPlan listet Pflichten mit Status und Frist', () => {
    const obligations = deriveObligations(system.classification, answers, CONTENT_PACK);
    const md = actionPlan(system, obligations, CONTENT_PACK, TODAY);
    expect(md).toContain('Risikomanagementsystem');
    expect(md).toContain('In Umsetzung');
    expect(md).toContain('02.12.2027');
  });

  it('annexIVSkeleton befüllt bekannte Felder und markiert offene Punkte', () => {
    const md = annexIVSkeleton(system, CONTENT_PACK, TODAY);
    expect(md).toContain('Vorauswahl von Bewerbungen');
    expect(md).toContain('_[AUSFÜLLEN]_');
    expect(md).toContain('Annex IV');
  });

  it('inventoryReport listet alle Systeme tabellarisch', () => {
    const md = inventoryReport([system], CONTENT_PACK, TODAY);
    expect(md).toContain('HR-Screening');
    expect(md).toContain('| System |');
    expect(md).toContain('Registrierte KI-Systeme: **1**');
  });

  it('übernimmt das Firmenprofil in den Berichtskopf', () => {
    const profile: CompanyProfile = {
      name: 'Muster GmbH',
      street: 'Musterstraße 1',
      zipCity: '60311 Frankfurt',
      contactPerson: 'Max Mustermann',
      email: 'compliance@muster.de',
      phone: '+49 69 000000',
      website: 'www.muster.de',
      logoDataUrl: '',
      countries: [],
    };
    const md = riskReport(system, CONTENT_PACK, TODAY, profile);
    expect(md).toContain('| **Unternehmen** | Muster GmbH |');
    expect(md).toContain('compliance@muster.de');
    expect(md).toContain('| **Ansprechpartner** | Max Mustermann |');
    expect(inventoryReport([system], CONTENT_PACK, TODAY, profile)).toContain('Muster GmbH · Stand');
  });

  it('lässt den Berichtskopf ohne Profil unverändert', () => {
    const md = riskReport(system, CONTENT_PACK, TODAY);
    expect(md).not.toContain('**Unternehmen**');
  });
});
