from src.workers.tasks.codegen import (
    _filter_user_visible_changes,
    _is_internal_codegen_path,
    _remaining_pending_user_input_items,
)


def test_is_internal_codegen_path_matches_ephemeral_codegen_files() -> None:
    assert _is_internal_codegen_path(".cursorrules") is True
    assert _is_internal_codegen_path(".p2hs/ai-context/system-prompt.md") is True
    assert _is_internal_codegen_path(".p2hs/ai-context/last-run.json") is True
    assert _is_internal_codegen_path("p2hs/app-blocks-plan.json") is True
    assert _is_internal_codegen_path(".p2hs.json") is True
    assert _is_internal_codegen_path("pages/register.vue") is False


def test_filter_user_visible_changes_drops_internal_only_paths() -> None:
    files, details, had_reported = _filter_user_visible_changes(
        [
            ".p2hs/ai-context/system-prompt.md",
            ".p2hs/ai-context/last-run.json",
        ],
        None,
    )

    assert had_reported is True
    assert files is None
    assert details is None


def test_filter_user_visible_changes_drops_platform_projections() -> None:
    files, details, had_reported = _filter_user_visible_changes(
        [
            "functions/_lib/p2hs-port-contract.mjs",
            "functions/api/ai/complete.js",
            "pages/index.vue",
        ],
        [
            {"path": "functions/api/auth/magic-link/consume.js", "change_type": "modified"},
            {"path": "pages/index.vue", "change_type": "modified"},
        ],
    )

    assert had_reported is True
    assert files == ["pages/index.vue"]
    assert details == [{"path": "pages/index.vue", "change_type": "modified"}]


def test_filter_user_visible_changes_preserves_customer_files() -> None:
    files, details, had_reported = _filter_user_visible_changes(
        [
            ".p2hs/ai-context/system-prompt.md",
            "pages/register.vue",
        ],
        [
            {"path": ".p2hs/ai-context/last-run.json", "change_type": "modified"},
            {"path": "components/registration/RegistrationFormField.vue", "change_type": "added"},
        ],
    )

    assert had_reported is True
    assert files == ["pages/register.vue"]
    assert details == [
        {
            "path": "components/registration/RegistrationFormField.vue",
            "change_type": "added",
        }
    ]


def test_remaining_pending_user_input_items_treats_existing_env_vars_as_satisfied() -> None:
    manifest = {
        "version": 1,
        "items": [
            {"id": "AGENCY_CONTACT_EMAIL", "type": "public"},
            {"id": "DESTINATION_COUNTRIES", "type": "public"},
        ],
    }

    remaining = _remaining_pending_user_input_items(
        manifest,
        builder_user_values={},
        env_var_keys={"AGENCY_CONTACT_EMAIL", "AGENCY_NAME", "AGENCY_PHONE"},
    )

    assert remaining == [{"id": "DESTINATION_COUNTRIES", "type": "public"}]
