"""Phase 2P — customer-bound google_docs provider foundation. No document.* flips."""
from __future__ import annotations

from src.domain.codegen.prompt_wrapper import _app_recipes
from src.domain.connectors.catalog import CONNECTORS, get_action, slugs_implementing
from src.domain.connectors.invoke_capabilities import (
    DOCUMENT_FAMILY_PHASE_2P,
    DOCUMENT_HYPOTHESIZED_VERBS_NOT_FROZEN,
    GOOGLE_DOCS_PROVIDER_ACTIONS,
    INVOKE_CAPABILITIES,
    is_implemented,
)
from src.domain.connectors.public_gateway import (
    PHASE_2P_STATUS,
    PUBLIC_INVOCABLE_CAPABILITIES,
)
from src.domain.manifest.service import _connectors_manifest_envelope


def test_phase_2p_is_complete_customer_bound_only() -> None:
    assert PHASE_2P_STATUS == "complete"
    phase = DOCUMENT_FAMILY_PHASE_2P
    assert phase["status"] == "complete"
    assert phase["execution_domain"] == "C"
    assert phase["connector"] == "google_docs"
    assert phase["implements"] is False
    assert phase["invocable_document_star"] is False
    assert phase["codegen_default"] is False
    assert phase["public_bff_capability_route"] is False
    assert phase["p2hs_owned_google_path"] is False
    assert phase["next_slice"] == "2f_document_runtime_truth"
    assert phase["actions"] == GOOGLE_DOCS_PROVIDER_ACTIONS


def test_google_docs_actions_do_not_implement_document_capabilities() -> None:
    connector = CONNECTORS["google_docs"]
    assert "customer" in connector.description.lower() or "byoc" in connector.description.lower()
    assert connector.secret_fields == (
        "access_token",
        "refresh_token",
        "client_id",
        "client_secret",
    )
    slugs = tuple(action.slug for action in connector.actions)
    assert slugs == GOOGLE_DOCS_PROVIDER_ACTIONS
    for slug in GOOGLE_DOCS_PROVIDER_ACTIONS:
        pair = get_action(slug)
        assert pair is not None
        assert pair[1].implements != ()
        assert pair[1].required_capabilities == ("integrations.execute",)
        assert "https://www.googleapis.com/auth/documents" in pair[1].required_scopes
    for verb in DOCUMENT_HYPOTHESIZED_VERBS_NOT_FROZEN:
        assert slugs_implementing(verb) == ()
        assert verb not in INVOKE_CAPABILITIES
        assert is_implemented(verb) is False
        assert verb not in PUBLIC_INVOCABLE_CAPABILITIES


def test_phase_2p_does_not_freeze_export_or_sheets() -> None:
    envelope = _connectors_manifest_envelope(items={})
    capabilities = envelope.get("capabilities") or {}
    for verb in DOCUMENT_HYPOTHESIZED_VERBS_NOT_FROZEN:
        assert verb not in capabilities
    recipes = _app_recipes()
    assert "document.export" not in recipes
    assert "spreadsheet.rows.read" in recipes  # Later Phase 3, not part of 2P.
