"""Tests for FRIA (Fundamental Rights Impact Assessment) template generator."""

import pytest
from fria_template import (
    generate_fria_draft,
    DOMAIN_RIGHTS_MAP,
    UNIVERSAL_RIGHTS,
    DEFAULT_MEASURES,
    HR_MEASURES,
    EDUCATION_MEASURES,
)


class TestGenerateFRIA:
    """Tests for generate_fria_draft()."""

    def test_minimal_context(self):
        ctx = {
            "organisation_name": "Test GmbH",
            "system_name": "AI Tool",
            "system_description": "KI-Assistenz",
            "einsatzzweck": "Automatisierung",
        }
        result = generate_fria_draft(ctx)
        assert result["ORGANISATION_NAME"] == "Test GmbH"
        assert result["SYSTEM_NAME"] == "AI Tool"
        assert result["AI_ACT_KLASSIFIKATION"] == "High-Risk"

    def test_hr_domain_rights(self):
        ctx = {
            "organisation_name": "HR Corp",
            "system_name": "Recruiting AI",
            "system_description": "Bewerber-Screening",
            "einsatzzweck": "Personalauswahl",
            "domain": "hr",
        }
        result = generate_fria_draft(ctx)

        # HR domain should include employment rights
        assert "Berufsfreiheit" in result["GRUNDRECHTE_ANALYSE"]
        assert "Nicht-Diskriminierung" in result["GRUNDRECHTE_ANALYSE"]
        assert result["HR_KONTEXT"] is True
        assert result["BILDUNGSKONTEXT"] is False

    def test_education_domain_rights(self):
        ctx = {
            "organisation_name": "Schule",
            "system_name": "Bewertungs-KI",
            "system_description": "Notenunterstuetzung",
            "einsatzzweck": "Leistungsbewertung",
            "domain": "education",
        }
        result = generate_fria_draft(ctx)

        assert "Recht auf Bildung" in result["GRUNDRECHTE_ANALYSE"]
        assert "Rechte des Kindes" in result["GRUNDRECHTE_ANALYSE"]
        assert result["BILDUNGSKONTEXT"] is True

    def test_healthcare_domain_rights(self):
        ctx = {
            "organisation_name": "Klinik",
            "system_name": "Diagnose-KI",
            "system_description": "Diagnoseunterstuetzung",
            "einsatzzweck": "Diagnostik",
            "domain": "healthcare",
        }
        result = generate_fria_draft(ctx)

        assert "Menschenwuerde" in result["GRUNDRECHTE_ANALYSE"]
        assert "Schutz personenbezogener Daten" in result["GRUNDRECHTE_ANALYSE"]

    def test_universal_rights_always_present(self):
        for domain in ["hr", "education", "healthcare", "finance", ""]:
            ctx = {
                "organisation_name": "Test",
                "system_name": "Tool",
                "system_description": "x",
                "einsatzzweck": "y",
                "domain": domain,
            }
            result = generate_fria_draft(ctx)
            assert "Schutz personenbezogener Daten" in result["GRUNDRECHTE_ANALYSE"]

    def test_hr_measures_included(self):
        ctx = {
            "organisation_name": "Test",
            "system_name": "Tool",
            "system_description": "x",
            "einsatzzweck": "y",
            "domain": "hr",
        }
        result = generate_fria_draft(ctx)

        for measure in HR_MEASURES:
            assert measure in result["MASSNAHMEN_LISTE"]

    def test_education_measures_included(self):
        ctx = {
            "organisation_name": "Test",
            "system_name": "Tool",
            "system_description": "x",
            "einsatzzweck": "y",
            "domain": "education",
        }
        result = generate_fria_draft(ctx)

        for measure in EDUCATION_MEASURES:
            assert measure in result["MASSNAHMEN_LISTE"]

    def test_public_entity_flag(self):
        ctx = {
            "organisation_name": "Behoerde",
            "system_name": "Tool",
            "system_description": "x",
            "einsatzzweck": "y",
            "is_public_entity": True,
        }
        result = generate_fria_draft(ctx)
        assert result["OEFFENTLICHE_STELLE"] is True

    def test_risk_matrix_generated(self):
        ctx = {
            "organisation_name": "Test",
            "system_name": "Tool",
            "system_description": "x",
            "einsatzzweck": "y",
            "domain": "hr",
            "risk_score": 60,
        }
        result = generate_fria_draft(ctx)

        assert result["RISIKOMATRIX"] != "{{RISIKOMATRIX}}"
        assert "Nicht-Diskriminierung" in result["RISIKOMATRIX"]

    def test_affected_groups(self):
        ctx = {
            "organisation_name": "Test",
            "system_name": "Tool",
            "system_description": "x",
            "einsatzzweck": "y",
            "affected_groups": ["Bewerber", "Beschaeftigte"],
            "affected_count": "~500 pro Jahr",
        }
        result = generate_fria_draft(ctx)

        assert "Bewerber" in result["BETROFFENE_GRUPPEN"]
        assert result["BETROFFENE_ANZAHL"] == "~500 pro Jahr"


class TestDSFADomainRisks:
    """Tests for domain-specific risks in DSFA generator."""

    def test_hr_domain_adds_agg_risks(self):
        # Import from dsfa_template
        from dsfa_template import _generate_risk_assessment

        ctx = {"has_ai_systems": True, "domain": "hr"}
        output = _generate_risk_assessment(ctx)

        assert "AGG-Verstoss" in output
        assert "Beweislastumkehr" in output

    def test_education_domain_adds_risks(self):
        from dsfa_template import _generate_risk_assessment

        ctx = {"has_ai_systems": True, "domain": "education"}
        output = _generate_risk_assessment(ctx)

        assert "Chancenungleichheit" in output

    def test_healthcare_domain_adds_risks(self):
        from dsfa_template import _generate_risk_assessment

        ctx = {"has_ai_systems": True, "domain": "healthcare"}
        output = _generate_risk_assessment(ctx)

        assert "Fehldiagnose" in output

    def test_finance_domain_adds_risks(self):
        from dsfa_template import _generate_risk_assessment

        ctx = {"has_ai_systems": True, "domain": "finance"}
        output = _generate_risk_assessment(ctx)

        assert "Kredit-Scoring" in output

    def test_no_domain_no_extra_risks(self):
        from dsfa_template import _generate_risk_assessment

        ctx = {"has_ai_systems": True}
        output = _generate_risk_assessment(ctx)

        assert "AGG-Verstoss" not in output
        assert "Fehldiagnose" not in output


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
