"""Helper script for Robot Framework correction model smoke tests."""

from __future__ import annotations

import sys
from pathlib import Path

# Ensure src is importable when run from workspace root
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))

from cleveragents.application.services.correction_service import CorrectionService
from cleveragents.domain.models.core.correction import (
    CorrectionMode,
    CorrectionStatus,
)


def _test_create_revert() -> None:
    """Create a correction in revert mode."""
    svc = CorrectionService()
    req = svc.request_correction(
        plan_id="plan-1",
        target_decision_id="DEC-001",
        mode=CorrectionMode.REVERT,
        guidance="Use FastAPI instead",
    )
    assert req.mode == CorrectionMode.REVERT
    assert req.status == CorrectionStatus.PENDING
    assert req.plan_id == "plan-1"
    print("correction-create-revert-ok")


def _test_create_append() -> None:
    """Create a correction in append mode."""
    svc = CorrectionService()
    req = svc.request_correction(
        plan_id="plan-1",
        target_decision_id="DEC-001",
        mode=CorrectionMode.APPEND,
        guidance="Add caching layer",
    )
    assert req.mode == CorrectionMode.APPEND
    assert req.status == CorrectionStatus.PENDING
    print("correction-create-append-ok")


def _test_dry_run() -> None:
    """Analyze impact in dry-run mode."""
    svc = CorrectionService()
    req = svc.request_correction(
        plan_id="plan-1",
        target_decision_id="DEC-001",
        mode=CorrectionMode.REVERT,
        guidance="Change framework",
        dry_run=True,
    )
    impact = svc.analyze_impact(req.correction_id)
    assert len(impact.affected_decisions) > 0
    assert impact.risk_level == "low"
    print(
        f"correction-dry-run-ok "
        f"(affected={len(impact.affected_decisions)}, "
        f"risk={impact.risk_level})"
    )


def _test_cancel() -> None:
    """Cancel a pending correction."""
    svc = CorrectionService()
    req = svc.request_correction(
        plan_id="plan-1",
        target_decision_id="DEC-001",
        mode=CorrectionMode.REVERT,
        guidance="To be cancelled",
    )
    cancelled = svc.cancel_correction(req.correction_id)
    assert cancelled.status == CorrectionStatus.CANCELLED
    print("correction-cancel-ok")


def _test_execute() -> None:
    """Execute a correction."""
    svc = CorrectionService()
    req = svc.request_correction(
        plan_id="plan-1",
        target_decision_id="DEC-001",
        mode=CorrectionMode.REVERT,
        guidance="Execute this",
    )
    result = svc.execute_correction(req.correction_id)
    assert result.status == CorrectionStatus.APPLIED
    assert "DEC-001" in result.reverted_decisions
    print(
        f"correction-execute-ok "
        f"(status={result.status.value}, "
        f"reverted={result.reverted_decisions})"
    )


def _test_list_by_plan() -> None:
    """List corrections filtered by plan."""
    svc = CorrectionService()
    svc.request_correction(
        plan_id="plan-1",
        target_decision_id="DEC-001",
        mode=CorrectionMode.REVERT,
        guidance="Fix A",
    )
    svc.request_correction(
        plan_id="plan-1",
        target_decision_id="DEC-002",
        mode=CorrectionMode.APPEND,
        guidance="Fix B",
    )
    svc.request_correction(
        plan_id="plan-2",
        target_decision_id="DEC-003",
        mode=CorrectionMode.REVERT,
        guidance="Fix C",
    )

    plan1 = svc.list_corrections(plan_id="plan-1")
    plan2 = svc.list_corrections(plan_id="plan-2")
    all_corrections = svc.list_corrections()

    assert len(plan1) == 2
    assert len(plan2) == 1
    assert len(all_corrections) == 3
    print(f"correction-list-ok (plan1={len(plan1)}, plan2={len(plan2)})")


def _test_validate_guidance() -> None:
    """Verify empty guidance is accepted (defaults to empty string)."""
    svc = CorrectionService()
    req = svc.request_correction(
        plan_id="plan-1",
        target_decision_id="DEC-001",
        mode=CorrectionMode.REVERT,
        guidance="",
    )
    assert req.guidance == ""
    assert req.status == CorrectionStatus.PENDING
    print("correction-validate-guidance-ok")


def _test_enums() -> None:
    """Verify enum values."""
    assert CorrectionMode.REVERT.value == "revert"
    assert CorrectionMode.APPEND.value == "append"
    assert CorrectionStatus.PENDING.value == "pending"
    assert CorrectionStatus.ANALYZING.value == "analyzing"
    assert CorrectionStatus.EXECUTING.value == "executing"
    assert CorrectionStatus.APPLIED.value == "applied"
    assert CorrectionStatus.FAILED.value == "failed"
    assert CorrectionStatus.CANCELLED.value == "cancelled"
    print("correction-enums-ok")


_TESTS = {
    "create_revert": _test_create_revert,
    "create_append": _test_create_append,
    "dry_run": _test_dry_run,
    "cancel": _test_cancel,
    "execute": _test_execute,
    "list_by_plan": _test_list_by_plan,
    "validate_guidance": _test_validate_guidance,
    "enums": _test_enums,
}

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print(f"Usage: {sys.argv[0]} <test_name>")
        print(f"Available tests: {', '.join(sorted(_TESTS))}")
        sys.exit(1)

    test_name = sys.argv[1]
    if test_name not in _TESTS:
        print(f"Unknown test: {test_name}")
        print(f"Available: {', '.join(sorted(_TESTS))}")
        sys.exit(1)

    _TESTS[test_name]()
