from src.models.drift import DriftItem, DriftLevel, DriftReport, DriftStatus
from src.services.drift_fingerprint_state_service import (
    build_drift_fingerprint_state,
)
from src.services.drift_repeat_state import build_drift_repeat_state


def _report(report_id: str = "drift-1", status: DriftStatus = DriftStatus.PENDING) -> DriftReport:
    return DriftReport(
        report_id=report_id,
        namespace="awoooi-prod",
        high_count=1,
        medium_count=32,
        info_count=23,
        status=status,
        items=[
            DriftItem(
                resource_kind="Deployment",
                resource_name="awoooi-api",
                namespace="awoooi-prod",
                field_path="spec.template.spec.volumes",
                git_value=["a"],
                actual_value=["a", "b"],
                drift_level=DriftLevel.HIGH,
            )
        ],
    )


def _no_drift_report(report_id: str = "no-drift-1") -> DriftReport:
    return DriftReport(
        report_id=report_id,
        namespace="awoooi-prod",
        high_count=0,
        medium_count=0,
        info_count=0,
        status=DriftStatus.IGNORED,
        items=[],
    )


def test_build_state_marks_repeated_pending_human() -> None:
    current = _report("drift-3")
    repeat = build_drift_repeat_state(
        current,
        [_report("drift-1"), _report("drift-2")],
    )

    state = build_drift_fingerprint_state(current, repeat)

    assert state["schema_version"] == "drift_fingerprint_state_v1"
    assert state["fingerprint"].startswith("dfp_")
    assert state["occurrences_12h"] == 3
    assert state["fsm_state"] == "pending_human_repeated"
    assert state["writes_drift_status"] is False
    assert state["writes_incident_state"] is False
    assert state["p0_escalation"]["dedup_window_hours"] == 24


def test_build_state_marks_zero_diff_open_pr() -> None:
    report = _report("drift-4")
    repeat = build_drift_repeat_state(report, [])
    state = build_drift_fingerprint_state(
        report,
        repeat,
        open_pr={
            "number": 145,
            "state": "open",
            "merged": False,
            "file_count": 0,
            "commit_count": 0,
            "is_zero_diff": True,
            "html_url": "http://gitea.local/wooo/awoooi/pulls/145",
        },
    )

    assert state["fsm_state"] == "pr_open_zero_diff"
    assert state["next_step"] == "close_zero_diff_pr_and_prepare_real_yaml_patch"
    assert state["open_pr"]["number"] == 145


def test_build_state_marks_handoff_recorded() -> None:
    report = _report("drift-5")
    repeat = build_drift_repeat_state(report, [])
    state = build_drift_fingerprint_state(
        report,
        repeat,
        latest_handoff={
            "handoff_kind": "manual_investigation",
            "handoff_status": "recorded",
        },
    )

    assert state["fsm_state"] == "handoff_recorded"
    assert state["next_step"] == "operator_review_handoff_and_execute_manual_plan"


def test_build_state_marks_verified_no_drift_remediation() -> None:
    report = _no_drift_report("no-drift-2")
    repeat = build_drift_repeat_state(report, [])
    state = build_drift_fingerprint_state(
        report,
        repeat,
        latest_remediation={
            "remediation_kind": "live_env_rollback",
            "remediation_status": "verified_no_drift",
            "verification_report_id": "no-drift-2",
        },
    )

    assert state["fsm_state"] == "no_drift_verified"
    assert state["next_step"] == "monitor_for_recurrence"
    assert state["latest_remediation"]["remediation_status"] == "verified_no_drift"
    assert state["writes_remediation_record"] is False


def test_build_state_marks_remediation_executed_unverified() -> None:
    report = _report("drift-6")
    repeat = build_drift_repeat_state(report, [])
    state = build_drift_fingerprint_state(
        report,
        repeat,
        latest_remediation={
            "remediation_kind": "live_env_rollback",
            "remediation_status": "executed_unverified",
        },
    )

    assert state["fsm_state"] == "remediation_executed_unverified"
    assert state["next_step"] == "run_verification_scan_then_record_result"
