"""Phase 4 storage.file.* is planned, not opened. Managed R2 ≠ customer storage."""
from __future__ import annotations

from src.domain.connectors.catalog import CONNECTORS, get_action
from src.domain.connectors.invoke_capabilities import (
    CAPABILITY_CONVERSION_PATTERN,
    EXECUTION_DOMAINS,
    INVOKE_CAPABILITIES,
    STORAGE_FAMILY_PLAN,
    STORAGE_HYPOTHESIZED_VERBS_NOT_FROZEN,
    is_implemented,
)
from src.domain.connectors.public_gateway import (
    PHASE_0_INVARIANTS,
    PHASE_4_STATUS,
    PUBLIC_INVOCABLE_CAPABILITIES,
)


def test_storage_family_is_planned_split_not_opened() -> None:
    pattern = CAPABILITY_CONVERSION_PATTERN
    assert PHASE_4_STATUS == "complete"
    assert pattern["phase_4_status"] == "complete"
    assert pattern["opened_families"]["storage.file.*"] == "split"
    assert pattern["next_candidate_classification"]["storage.file.*"] == "split_A_and_B"
    plan = STORAGE_FAMILY_PLAN
    assert plan["opened"] is True
    assert plan["verbs_frozen"] is True
    assert plan["classification_when_opened"] == "split"
    assert plan["providers_when_opened"] == ("cloudflare_r2", "amazon_s3")
    assert plan["byoc_r2_catalog_exists"] is True
    assert plan["byoc_s3_catalog_exists"] is True
    assert plan["managed_r2_in_scope"] is False
    assert plan["customer_absent_does_not_use_managed_r2"] is True
    assert plan["working_model"]["managed_r2_is_not_an_implementation"] is True
    assert plan["working_model"]["unique_without_alias"] == "resource_unbound"
    assert "first-wins" in plan["working_model"]["multiple"]


def test_storage_verbs_are_candidates_not_capabilities() -> None:
    assert STORAGE_HYPOTHESIZED_VERBS_NOT_FROZEN == (
        "storage.file.put",
        "storage.file.get",
        "storage.file.delete",
        "storage.file.exists",
        "storage.file.list",
    )
    for verb in STORAGE_HYPOTHESIZED_VERBS_NOT_FROZEN:
        assert verb in INVOKE_CAPABILITIES
        assert is_implemented(verb) is True
        assert verb in PUBLIC_INVOCABLE_CAPABILITIES


def test_byoc_r2_exists_s3_does_not_managed_r2_is_out_of_scope() -> None:
    assert "cloudflare_r2" in CONNECTORS
    assert "your own" in CONNECTORS["cloudflare_r2"].description.lower()
    assert "amazon_s3" in CONNECTORS
    assert get_action("amazon_s3.put_object") is not None
    assert "p2hs_managed_r2" in STORAGE_FAMILY_PLAN["phase_4a_out_of_scope"]
    assert "prompt_attachments" in STORAGE_FAMILY_PLAN["phase_4a_out_of_scope"]
    assert "cloudflare_r2_byoc_credentials" in STORAGE_FAMILY_PLAN["phase_4a_in_scope"]
    assert "amazon_s3_cloud_handlers" in STORAGE_FAMILY_PLAN["phase_4a_in_scope"]


def test_managed_and_customer_domains_are_independent_not_fallbacks() -> None:
    pattern = CAPABILITY_CONVERSION_PATTERN
    assert PHASE_0_INVARIANTS["managed_vs_customer"] == (
        "parallel_independent_domains_not_fallbacks"
    )
    assert pattern["managed_vs_customer_invariant"].startswith(
        "Internal use of provider X"
    )
    assert pattern["parallel_execution_domain_invariant"].startswith(
        "P2HS-owned execution"
    )
    assert pattern["execution_domains_are_not_fallbacks"] is True
    assert pattern["no_parallel_execution_domain_registry"] is True
    domains = EXECUTION_DOMAINS
    assert domains["model"] == (
        "provider",
        "p2hs_owned_execution",
        "customer_bound_execution",
    )
    assert domains["both_means"] == "independent_parallel_paths_not_choose_one"
    assert domains["not_defined_by"] == "where_the_secret_is_stored"
    assert set(domains["defined_by"]) == {
        "ownership",
        "resource_lifecycle",
        "authorization_authority",
        "operational_purpose",
    }
    assert domains["p2hs_owned_execution"]["resolver_may_inspect_customer_connections"] is False
    assert domains["customer_bound_execution"]["resolver_may_inspect_p2hs_owned_resources"] is False
    assert domains["managed_r2_does_not_implement_storage_file"] is True
    assert domains["customer_r2_does_not_replace_managed_r2"] is True
    assert "customer_r2_absent_then_managed_r2_for_storage_file" in domains["never"]
    assert "customer_github_for_p2hs_repo_pipeline" in domains["never"]
    assert ("github", "B") in domains["same_provider_both_independent"]
    assert ("cloudflare", "B") in domains["same_provider_both_independent"]
    assert domains["family_open_step_0"][0] == "label_each_existing_provider_path_P_C_or_B"
    assert pattern["operating_sequence"][0] == "execution_domain_inventory"
