from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def read(path: str) -> str:
    return (ROOT / path).read_text(encoding="utf-8")


def test_review_manually_and_remove_from_list_are_operational_pending_actions():
    task_service = read("app/task_service.py")
    action_catalog = read("app/action_catalog.py")

    assert "def _initial_task_status" in task_service
    assert 'code in {"IGNORE_SPAM", "NO_ACTION"}' in task_service
    assert 'code in {"REVIEW_MANUALLY", "REMOVE_FROM_LIST"}' in task_service
    assert '"REVIEW_MANUALLY": {\n        "route": "rever",\n        "action_required": True' in action_catalog
    assert '"REMOVE_FROM_LIST": {\n        "route": "marketing",\n        "action_required": True' in action_catalog


def test_reclassify_review_stays_pending_and_spam_is_skipped():
    task_service = read("app/task_service.py")
    assert 'new_status = "skipped" if new_action_code in {"IGNORE_SPAM", "NO_ACTION"} else "pending"' in task_service
    assert '"REMOVE_FROM_LIST": ("marketing", "Remover contacto da lista")' in task_service


def test_operations_work_items_include_chatwoot_context_and_url():
    operations = read("app/operations_service.py")
    operations_page = read("app/admin_ui/pages/operations.py")

    assert "build_chatwoot_conversation_url" in operations
    assert "chatwoot_url" in operations
    assert "t.source_system" in operations
    assert "t.conversation_id" in operations
    assert "Chatwoot ↗" in operations_page
    assert "O Chatwoot é a inbox" in operations_page


def test_communications_not_primary_navigation_but_routes_stay_available():
    navigation = read("app/admin_ui/navigation.py")
    communications = read("app/admin_ui/pages/communications.py")
    operations = read("app/admin_ui/pages/operations.py")
    assert '("communications", "/communications", "bi-envelope", "Comunicações")' not in navigation
    assert '@router.get("/communications"' in communications
    assert 'Chatwoot continua a ser a inbox' in communications or 'O Chatwoot é a inbox' in operations


def test_reopen_chatwoot_review_tasks_script_exists():
    script = read("scripts/reopen_chatwoot_review_tasks.py")
    assert "REVIEW_MANUALLY" in script
    assert "REMOVE_FROM_LIST" in script
    assert "--dry-run" in script
    assert "status = 'pending'" in script
