from pathlib import Path

import scripts.api_contract_guard as guard


def test_api_contract_guard_allows_clean_api_tree(tmp_path: Path, monkeypatch) -> None:
    monkeypatch.setattr(guard, "REPO_ROOT", tmp_path)
    api_root = tmp_path / "grantflow" / "api"
    api_root.mkdir(parents=True)
    (api_root / "runtime_state.py").write_text(
        "JOB_STORE = object()\n",
        encoding="utf-8",
    )

    assert guard._forbidden_app_module_bridge_refs(api_root) == []


def test_api_contract_guard_reports_app_module_bridge(tmp_path: Path, monkeypatch) -> None:
    monkeypatch.setattr(guard, "REPO_ROOT", tmp_path)
    api_root = tmp_path / "grantflow" / "api"
    api_root.mkdir(parents=True)
    (api_root / "legacy.py").write_text(
        "def _app_module():\n"
        "    raise RuntimeError\n"
        "from grantflow.api import app as api_app_module\n"
        "store = _app_module().JOB_STORE\n",
        encoding="utf-8",
    )

    findings = guard._forbidden_app_module_bridge_refs(api_root)

    assert findings == [
        "grantflow/api/legacy.py:1: def _app_module():",
        "grantflow/api/legacy.py:3: from grantflow.api import app as api_app_module",
        "grantflow/api/legacy.py:4: store = _app_module().JOB_STORE",
    ]


def test_api_contract_guard_allows_public_app_import_in_tests(tmp_path: Path, monkeypatch) -> None:
    monkeypatch.setattr(guard, "REPO_ROOT", tmp_path)
    tests_root = tmp_path / "grantflow" / "tests"
    tests_root.mkdir(parents=True)
    (tests_root / "test_public_app.py").write_text(
        "from grantflow.api.app import app\n" "\n" "def test_app_title():\n" "    assert app.title\n",
        encoding="utf-8",
    )

    assert guard._forbidden_test_app_private_refs(tests_root) == []


def test_api_contract_guard_reports_private_app_import_in_tests(tmp_path: Path, monkeypatch) -> None:
    monkeypatch.setattr(guard, "REPO_ROOT", tmp_path)
    tests_root = tmp_path / "grantflow" / "tests"
    tests_root.mkdir(parents=True)
    (tests_root / "test_bad_private_import.py").write_text(
        "from grantflow.api.app import _resolve_export_inputs\n" "import grantflow.api.app as api_app_module\n",
        encoding="utf-8",
    )

    findings = guard._forbidden_test_app_private_refs(tests_root)

    assert findings == [
        "grantflow/tests/test_bad_private_import.py:1: private app import(s): _resolve_export_inputs",
        "grantflow/tests/test_bad_private_import.py:2: app module import is reserved for import-contract tests",
    ]


def test_api_contract_guard_allows_import_contract_private_app_import(tmp_path: Path, monkeypatch) -> None:
    monkeypatch.setattr(guard, "REPO_ROOT", tmp_path)
    tests_root = tmp_path / "grantflow" / "tests"
    tests_root.mkdir(parents=True)
    (tests_root / "test_import_contract.py").write_text(
        "from grantflow.api.app import _resolve_export_inputs\n" "import grantflow.api.app as api_app_module\n",
        encoding="utf-8",
    )

    assert guard._forbidden_test_app_private_refs(tests_root) == []
