"""Integration tests for agentkit site (D4) — v0.83.0."""
from __future__ import annotations

import json
import tempfile
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest


# ---------------------------------------------------------------------------
# D4: run --site integration
# ---------------------------------------------------------------------------

def test_run_cmd_accepts_site_dir_param():
    """run_command should accept a site_dir kwarg without error."""
    import inspect
    from agentkit_cli.commands.run_cmd import run_command
    sig = inspect.signature(run_command)
    assert "site_dir" in sig.parameters


def test_run_cmd_site_dir_updates_index(tmp_path):
    """run_command site_dir logic: invoking _site_engine directly via the site_engine module."""
    # Rather than fully mocking run_command (complex signature), verify the code path
    # that run_command uses for site_dir works correctly in isolation.
    from agentkit_cli.site_engine import SiteEngine
    from agentkit_cli.history import HistoryDB
    db = HistoryDB(db_path=tmp_path / "e.db")
    engine = SiteEngine(db=db)
    site_dir = tmp_path / "site"
    site_dir.mkdir()
    index_page = engine.generate_index()
    (site_dir / "index.html").write_text(index_page.html, encoding="utf-8")
    assert (site_dir / "index.html").exists()
    assert "<html" in (site_dir / "index.html").read_text()


def test_share_command_detects_site_dir(tmp_path):
    """share_command: if path/index.html exists, treat as site dir."""
    # This just tests that site_cmd share path works for sites
    from agentkit_cli.commands.site_cmd import site_command
    out = tmp_path / "s"
    upload_calls = []
    def fake_upload(html):
        upload_calls.append(True)
        return "https://here.now/abc"
    site_command(output_dir=str(out), topics="go", quiet=True, share=True, db_path=tmp_path / "e.db", _upload_fn=fake_upload)
    assert len(upload_calls) == 1


def test_site_engine_imported_in_run_cmd():
    """Verify site_engine import works within run_cmd module."""
    from agentkit_cli.commands import run_cmd
    # Importing run_cmd should not crash
    assert hasattr(run_cmd, "run_command")


def test_site_generates_valid_json_ld(tmp_path):
    """All generated pages must contain parseable JSON-LD."""
    from agentkit_cli.site_engine import SiteEngine, SiteConfig
    from agentkit_cli.history import HistoryDB
    db = HistoryDB(db_path=tmp_path / "d.db")
    db.record_run("myorg/myrepo", "agentlint", 77.0, label="python")
    engine = SiteEngine(config=SiteConfig(topics=["python"]), db=db)
    result = engine.generate_site(tmp_path / "out", topics=["python"], limit=5)
    import re
    for page in result.pages:
        ld_matches = re.findall(r'<script type="application/ld\+json">(.*?)</script>', page.html, re.DOTALL)
        assert ld_matches, f"No JSON-LD in {page.path}"
        for match in ld_matches:
            parsed = json.loads(match)
            assert "@context" in parsed
            assert "@type" in parsed


def test_site_sitemap_count_matches_pages(tmp_path):
    """sitemap.xml must contain one <url> per generated page."""
    from agentkit_cli.site_engine import SiteEngine, SiteConfig
    from agentkit_cli.history import HistoryDB
    db = HistoryDB(db_path=tmp_path / "d.db")
    engine = SiteEngine(config=SiteConfig(topics=["python", "go"]), db=db)
    result = engine.generate_site(tmp_path / "o", topics=["python", "go"], limit=5)
    url_count = result.sitemap_xml.count("<url>")
    assert url_count == len(result.pages)


def test_version_is_083():
    from agentkit_cli import __version__
    # Accept any version >= 0.83.0 (forward-compatible)
    major, minor, patch = [int(x) for x in __version__.split(".")]
    assert (major, minor) >= (0, 83), f"Expected version >= 0.83.0, got {__version__}"


def test_pyproject_version(tmp_path):
    """pyproject.toml must match current version."""
    import subprocess
    from agentkit_cli import __version__
    repo = Path(__file__).parent.parent
    result = subprocess.run(
        ["grep", "^version", "pyproject.toml"],
        cwd=repo,
        capture_output=True,
        text=True,
    )
    assert __version__ in result.stdout
