"""Tests for CLI audit-report subcommand."""

from __future__ import annotations

import json
from unittest.mock import patch

from orxaq.experiences.cli import main as cli_main
from orxaq.kernel.audit import AuditLog


def _seed_audit_log(monkeypatch):
    """Seed OrxaqEngine's audit_log with test entries."""
    log = AuditLog()
    for i in range(10):
        actions = ["create", "update", "delete", "query"]
        actors = ["system", "user:alice", "user:bob"]
        log.append(
            action=actions[i % len(actions)],
            actor=actors[i % len(actors)],
            details=f"operation {i} on entity e{i}",
        )

    def patched_init(self):
        self.audit_log = log

    monkeypatch.setattr(
        "orxaq.experiences.cli.OrxaqEngine.__init__", patched_init
    )


class TestAuditReportFull:
    def test_full_json(self, monkeypatch, capsys):
        _seed_audit_log(monkeypatch)
        with patch("sys.argv", [
            "orxaq", "--json", "audit-report", "full",
        ]):
            rc = cli_main()
        assert rc == 0
        out = json.loads(capsys.readouterr().out)
        assert out["entry_count"] == 10
        assert out["integrity"]["valid"] is True

    def test_full_text(self, monkeypatch, capsys):
        _seed_audit_log(monkeypatch)
        with patch("sys.argv", ["orxaq", "audit-report", "full"]):
            rc = cli_main()
        assert rc == 0
        out = capsys.readouterr().out
        assert "Audit Trail Report" in out

    def test_full_filtered(self, monkeypatch, capsys):
        _seed_audit_log(monkeypatch)
        with patch("sys.argv", [
            "orxaq", "--json", "audit-report", "full",
            "--action", "create",
        ]):
            rc = cli_main()
        assert rc == 0
        out = json.loads(capsys.readouterr().out)
        assert out["entry_count"] < 10


class TestAuditReportCompliance:
    def test_compliance_json(self, monkeypatch, capsys):
        _seed_audit_log(monkeypatch)
        with patch("sys.argv", [
            "orxaq", "--json", "audit-report", "compliance",
        ]):
            rc = cli_main()
        assert rc == 0
        out = json.loads(capsys.readouterr().out)
        assert "SR11-7" in out["title"]
        assert out["integrity"]["valid"] is True

    def test_compliance_ccar(self, monkeypatch, capsys):
        _seed_audit_log(monkeypatch)
        with patch("sys.argv", [
            "orxaq", "--json", "audit-report", "compliance",
            "--framework", "ccar",
        ]):
            rc = cli_main()
        assert rc == 0
        out = json.loads(capsys.readouterr().out)
        assert "CCAR" in out["title"]


class TestAuditReportVerify:
    def test_verify_json(self, monkeypatch, capsys):
        _seed_audit_log(monkeypatch)
        with patch("sys.argv", [
            "orxaq", "--json", "audit-report", "verify",
        ]):
            rc = cli_main()
        assert rc == 0
        out = json.loads(capsys.readouterr().out)
        assert out["valid"] is True
        assert out["total_entries"] == 10

    def test_verify_text(self, monkeypatch, capsys):
        _seed_audit_log(monkeypatch)
        with patch("sys.argv", ["orxaq", "audit-report", "verify"]):
            rc = cli_main()
        assert rc == 0
        out = capsys.readouterr().out
        assert "VALID" in out


class TestAuditReportActor:
    def test_actor_json(self, monkeypatch, capsys):
        _seed_audit_log(monkeypatch)
        with patch("sys.argv", [
            "orxaq", "--json", "audit-report", "actor", "system",
        ]):
            rc = cli_main()
        assert rc == 0
        out = json.loads(capsys.readouterr().out)
        assert out["entry_count"] > 0
        assert "system" in out["title"]

    def test_actor_text(self, monkeypatch, capsys):
        _seed_audit_log(monkeypatch)
        with patch("sys.argv", [
            "orxaq", "audit-report", "actor", "user:alice",
        ]):
            rc = cli_main()
        assert rc == 0
        out = capsys.readouterr().out
        assert "user:alice" in out
