from __future__ import annotations

import json
import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

from scripts.runtime.source_cache import cache_sources, is_important_source


class FakeResponse:
    headers = {"content-type": "text/html; charset=utf-8"}
    url = "https://www.fda.gov/example"
    content = b"<html><body><h1>FDA Guidance</h1><p>Important CGMP text.</p></body></html>"

    def raise_for_status(self) -> None:
        return None


class FakeClient:
    def __enter__(self) -> "FakeClient":
        return self

    def __exit__(self, *_args) -> None:
        return None

    def get(self, url: str) -> FakeResponse:
        assert url == "https://www.fda.gov/example"
        return FakeResponse()

    def close(self) -> None:
        return None


def test_is_important_source_detects_official_regulator() -> None:
    assert is_important_source({"url": "https://www.fda.gov/example", "title": "FDA"})
    assert not is_important_source({"url": "https://example.com/blog", "title": "Blog"})


def test_cache_sources_writes_markdown_and_updates_registry(tmp_path: Path, monkeypatch) -> None:
    project = tmp_path / "project"
    sources = project / "phase2" / "sources.jsonl"
    sources.parent.mkdir(parents=True)
    sources.write_text(
        json.dumps(
            {
                "id": "src_fda_001",
                "title": "FDA Guidance",
                "url": "https://www.fda.gov/example",
                "tier": "Tier 1",
            },
            ensure_ascii=False,
        )
        + "\n",
        encoding="utf-8",
    )

    monkeypatch.setattr("scripts.runtime.source_cache.httpx.Client", lambda **_kwargs: FakeClient())

    results = cache_sources(project)

    rows = [json.loads(line) for line in sources.read_text(encoding="utf-8").splitlines()]
    assert len(results) == 1
    assert rows[0]["cached_text_path"].startswith("phase2/source_cache/md/")
    cached = project / rows[0]["cached_text_path"]
    assert cached.exists()
    assert "Important CGMP text." in cached.read_text(encoding="utf-8")
