from __future__ import annotations

from src.domain.codegen.entity_ingest_adapter import (
    entity_ingest_adapter_files,
    webhook_secret_env_key,
)

_BINDING = {
    "pack_slug": "content-ingest",
    "provider": "content_ingest",
    "forward_path": "/api/webhooks/content-ingest",
}


def test_secret_env_key_matches_core_service_py_computation():
    # Mirrors feature_packs/service.py's env_key computation exactly -- both
    # sides must agree or the generated route can never read the right secret.
    pack_slug, provider = "content-ingest", "content_ingest"
    expected = (
        "P2HS_WEBHOOK_SECRET_"
        + "_".join(part.upper() for part in (pack_slug, provider) if part)
    ).replace("-", "_")
    assert webhook_secret_env_key(pack_slug, provider) == expected
    assert expected == "P2HS_WEBHOOK_SECRET_CONTENT_INGEST_CONTENT_INGEST"


def test_no_bindings_yields_no_files():
    assert entity_ingest_adapter_files("html", []) == {}
    assert entity_ingest_adapter_files("nuxt_app", []) == {}


def test_unsupported_project_type_yields_no_files():
    assert entity_ingest_adapter_files("shopify_theme", [_BINDING]) == {}


def test_html_and_react_install_only_pages_functions_variant():
    for project_type in ("html", "react_app"):
        files = entity_ingest_adapter_files(project_type, [_BINDING])
        assert set(files) == {
            "functions/_lib/p2hs-entity-ingest.mjs",
            "functions/api/webhooks/content-ingest.js",
        }
        route = files["functions/api/webhooks/content-ingest.js"]
        assert 'PACK_SLUG = "content-ingest"' in route
        assert 'PROVIDER = "content_ingest"' in route
        assert 'SECRET_ENV_KEY = "P2HS_WEBHOOK_SECRET_CONTENT_INGEST_CONTENT_INGEST"' in route
        assert '"../../_lib/p2hs-entity-ingest.mjs"' in route
        assert "__P2HS_ENTITY_INGEST" not in route


def test_nuxt_installs_both_pages_functions_and_native_route():
    files = entity_ingest_adapter_files("nuxt_app", [_BINDING])
    assert set(files) == {
        "functions/_lib/p2hs-entity-ingest.mjs",
        "functions/api/webhooks/content-ingest.js",
        "server/utils/p2hs-entity-ingest.mjs",
        "server/api/webhooks/content-ingest.post.ts",
    }
    nuxt_route = files["server/api/webhooks/content-ingest.post.ts"]
    assert '"../../utils/p2hs-entity-ingest.mjs"' in nuxt_route
    assert "__P2HS_ENTITY_INGEST" not in nuxt_route


def test_multiple_bindings_each_get_their_own_route_file():
    bindings = [
        _BINDING,
        {
            "pack_slug": "future-jobs-board",
            "provider": "jobs_ingest",
            "forward_path": "/api/ingest/jobs",
        },
    ]
    files = entity_ingest_adapter_files("html", bindings)
    assert "functions/api/webhooks/content-ingest.js" in files
    assert "functions/api/ingest/jobs.js" in files
    jobs_route = files["functions/api/ingest/jobs.js"]
    assert 'PACK_SLUG = "future-jobs-board"' in jobs_route
    assert 'PROVIDER = "jobs_ingest"' in jobs_route
