"""Tests for :func:`mgf.vm.forensics.render.render_case_report`."""

from __future__ import annotations

from datetime import UTC, datetime
from typing import TYPE_CHECKING
from uuid import uuid4

import pytest

from mgf.vm.forensics.case import Case, CaseStatus
from mgf.vm.forensics.render import render_case_report
from mgf.vm.forensics.store import CaseStore

if TYPE_CHECKING:
    from pathlib import Path


def _make_case(name: str = "alpha") -> Case:
    return Case(
        name=name,
        case_id=uuid4(),
        vm_name="suspect-1",
        status=CaseStatus.PREPARING,
        created_at=datetime(2026, 4, 26, 12, 0, tzinfo=UTC),
    )


@pytest.fixture
def store(tmp_path: Path) -> CaseStore:
    s = CaseStore(root=tmp_path / "forensics")
    s.create(_make_case("alpha"))
    return s


class TestRenderCaseReport:
    def test_writes_markdown_file_under_case_reports(
        self,
        store: CaseStore,
    ) -> None:
        target = render_case_report(store, "alpha")
        assert target.exists()
        assert target.parent.name == "reports"
        body = target.read_text(encoding="utf-8")
        assert "# Forensics Report: alpha" in body

    def test_filename_carries_utc_timestamp_prefix(self, store: CaseStore) -> None:
        target = render_case_report(store, "alpha")
        assert target.name.startswith("report-")
        assert target.suffix == ".md"

    def test_refreshes_manifest_so_verify_passes(self, store: CaseStore) -> None:
        target = render_case_report(store, "alpha")
        from mgf.vm.forensics.manifest import verify_manifest

        case_dir = target.parent.parent
        result = verify_manifest(case_dir)
        assert result.is_clean()

    def test_unknown_case_raises(self, store: CaseStore) -> None:
        from mgf.vm.core.exceptions import VManagerError

        with pytest.raises(VManagerError):
            render_case_report(store, "no-such-case")
