"""Report generation service tests (FASE-2.3)."""

import uuid
from unittest.mock import patch

import pytest

from app.domain.exceptions import EntityNotFoundError
from app.models.campaign import Campaign, CampaignTest
from app.models.enums import TestState
from app.models.technique import Technique
from app.models.test import Test


@patch("app.services.report_generation_service.report_engine.generate_pdf")
def test_generate_purple_campaign_report_pdf(mock_pdf, db, admin_user):
    mock_pdf.return_value = "/tmp/fake.pdf"

    technique = Technique(
        mitre_id="T1059.001",
        name="PowerShell",
        tactic="execution",
    )
    db.add(technique)
    db.flush()

    campaign = Campaign(name="Q1 Purple", description="Scope", status="active")
    db.add(campaign)
    db.flush()

    test = Test(
        name="PS test",
        technique_id=technique.id,
        state=TestState.validated,
        created_by=admin_user.id,
    )
    db.add(test)
    db.flush()
    db.add(CampaignTest(campaign_id=campaign.id, test_id=test.id))
    db.commit()

    path = __import__(
        "app.services.report_generation_service",
        fromlist=["generate_purple_campaign_report"],
    ).generate_purple_campaign_report(db, str(campaign.id), "pdf")

    assert path == "/tmp/fake.pdf"
    mock_pdf.assert_called_once()
    context = mock_pdf.call_args[0][1]
    assert context["tests_validated"] == 1
    assert len(context["tests"]) == 1


def test_generate_technique_detail_not_found(db):
    from app.services.report_generation_service import generate_technique_detail_report

    with pytest.raises(EntityNotFoundError):
        generate_technique_detail_report(db, str(uuid.uuid4()), "html")
