"""Preserve the historical 3A evidence while Phase 3/4 runtime now exists."""

import json
from pathlib import Path

from scripts.audit_capability_modernization import host_exists
from src.domain.connectors.catalog import CONNECTORS, slugs_implementing
from src.domain.connectors.invoke_capabilities import (
    CAPABILITY_CONVERSION_PATTERN,
    INVOKE_CAPABILITIES,
    capability_surface,
)
from src.domain.connectors.public_gateway import PUBLIC_INVOCABLE_CAPABILITIES
from src.domain.manifest.service import _connectors_manifest_envelope


def test_audit_opens_phase_three_without_freezing_contracts():
    state = CAPABILITY_CONVERSION_PATTERN
    assert state["current_program_phase"] == 8
    assert state["phase_3a_status"] == "complete"
    assert state["phase_3p_status"] == "complete"
    assert state["phase_3b_status"] == "complete"
    assert "spreadsheet.*" not in state["closed_families"]
    assert state["next_program_slice"] == "9_app_block_modernization"


def test_google_identity_and_other_google_connectors_do_not_supply_sheets():
    assert "google_docs" in CONNECTORS
    assert "google_calendar" in CONNECTORS
    audit = json.loads(
        (
            Path(__file__).resolve().parents[1] / "docs/audits/post_2g_migration_inventory.json"
        ).read_text(encoding="utf-8")
    )
    assert audit["sheets"]["customer_catalog_exists"] is False
    assert audit["sheets"]["verbs_frozen"] is False
    # Later row operations use only the real customer Sheets connector.
    for candidate in (
        "spreadsheet.rows.read",
        "spreadsheet.rows.append",
        "spreadsheet.rows.update",
    ):
        assert all(slug.startswith("google_sheets.") for slug in slugs_implementing(candidate))
    assert "spreadsheet.create" not in INVOKE_CAPABILITIES
    assert "spreadsheet.get" not in PUBLIC_INVOCABLE_CAPABILITIES


def test_codegen_does_not_advertise_sheets_and_preserves_docs_defaults():
    envelope = _connectors_manifest_envelope(items={})
    assert envelope["capabilities"]["spreadsheet.rows.read"]["role"] == "default"
    for verb in ("create", "get", "insert_text", "append_text", "replace_text"):
        capability = f"document.{verb}"
        observed = list(slugs_implementing(capability))
        surface = capability_surface(capability, observed)
        assert surface["status"] == "implemented"
        assert surface["role"] == "default"
        assert surface["invocable"] is True
        assert surface["implemented_by"] == observed
        assert surface["method"] == f"POST /api/projects/{{project_id}}/capabilities/{capability}"


def test_host_audit_resolves_fragments_and_rejects_missing_exports(monkeypatch):
    root = Path("scaffold")
    monkeypatch.setattr(Path, "is_file", lambda path: path == root / "blocks.jsx")
    monkeypatch.setattr(
        Path,
        "read_text",
        lambda path, **kwargs: "export function ContactForm() { return null; }",
    )
    assert host_exists(root, "blocks.jsx#ContactForm")
    assert host_exists(root, "blocks.jsx")
    assert not host_exists(root, "blocks.jsx#MissingForm")
    assert not host_exists(root, "absent.jsx#ContactForm")
