from __future__ import annotations

from dataclasses import dataclass
from datetime import UTC, datetime

from src.services.agent_replay_fixture import REDACTED, build_agent_replay_fixture


@dataclass
class _Incident:
    incident_id: str = "INC-001"
    severity: str = "P1"
    status: str = "resolved"
    alertname: str = "PodCrashLooping"
    alert_category: str = "kubernetes"
    notification_type: str = "TYPE-2"
    affected_services: list[str] | None = None
    signals: list[dict] | None = None
    frequency_snapshot: dict | None = None
    created_at: datetime | None = None
    updated_at: datetime | None = None
    resolved_at: datetime | None = None
    closed_at: datetime | None = None


@dataclass
class _Evidence:
    evidence_summary: str = "Pod restart spike"
    mcp_health: dict | None = None
    sensors_attempted: int = 3
    sensors_succeeded: int = 3
    historical_context: str = "Similar incident recovered after rollout restart"
    dependency_topology: dict | None = None
    business_metrics: dict | None = None
    verification_result: str | None = "success"
    self_healing_score: float | None = 0.9


@dataclass
class _Execution:
    success: bool = True
    playbook_name: str = "rollout restart checkout"
    executed_steps: list[str] | None = None
    error_message: str | None = None


def test_fixture_separates_context_from_labels_and_redacts_secrets():
    fixture = build_agent_replay_fixture(
        run_id="fixtures",
        incident=_Incident(
            affected_services=["checkout"],
            signals=[
                {
                    "labels": {
                        "alertname": "PodCrashLooping",
                        "authorization": "Bearer live-token",
                    },
                    "annotations": {"summary": "pod failed"},
                }
            ],
            frequency_snapshot={"api_key": "secret-value"},
            created_at=datetime(2026, 6, 1, tzinfo=UTC),
        ),
        evidence=_Evidence(
            mcp_health={"k8s": True, "token": "abc"},
            business_metrics={"orders": 10, "password": "do-not-export"},
        ),
        execution=_Execution(
            executed_steps=["kubectl rollout restart deployment checkout -n prod"],
            error_message="failed with Basic abc",
        ),
        agent_turn_count=4,
    ).to_dict()

    assert fixture["schema_version"] == "agent_replay_fixture_v1"
    assert fixture["incident_context"]["signals"][0]["labels"]["authorization"] == REDACTED
    assert fixture["incident_context"]["frequency_snapshot"]["api_key"] == REDACTED
    assert fixture["incident_context"]["mcp_health"]["token"] == REDACTED
    assert fixture["incident_context"]["business_metrics"]["password"] == REDACTED
    assert fixture["evaluation_labels"]["execution_error"] == REDACTED
    assert fixture["evaluation_labels"]["verification_result"] == "success"
    assert fixture["evaluation_labels"]["expected_action_markers"] == [
        "rollout restart",
        "checkout",
    ]
    assert "verification_result" not in fixture["incident_context"]
    assert fixture["source_metadata"]["agent_turn_count"] == 4
