import netloom.cli.help as helpmod


def test_render_action_block_includes_dynamic_body_metadata():
    text = helpmod.render_action_block(
        "add",
        {
            "method": "POST",
            "paths": ["/api/example"],
            "summary": "Create an example object",
            "response_codes": ["201 Created", "422 Unprocessable Entity"],
            "response_content_types": ["application/json"],
            "body_description": "Example payload",
            "body_required": ["name"],
            "body_fields": [
                {
                    "name": "name",
                    "type": "string",
                    "required": True,
                    "description": "Unique object name",
                }
            ],
            "body_example": {"name": "demo"},
        },
    )

    assert "summary: Create an example object" in text
    assert "response codes:" in text
    assert "body required:" in text
    assert '"name": "demo"' in text


def test_render_action_block_hides_params_when_body_fields_exist():
    text = helpmod.render_action_block(
        "add",
        {
            "method": "POST",
            "paths": ["/api/example"],
            "params": ["name", "description"],
            "body_fields": [
                {"name": "name", "type": "string", "required": True},
            ],
        },
    )

    assert "body fields:" in text
    assert "params:" not in text


def test_render_action_block_keeps_params_without_body_fields():
    text = helpmod.render_action_block(
        "list",
        {
            "method": "GET",
            "paths": ["/api/example"],
            "params": ["limit", "offset"],
        },
    )

    assert "params:" in text
    assert "limit" in text


def test_render_action_block_indents_multiline_notes():
    text = helpmod.render_action_block(
        "list",
        {
            "method": "GET",
            "paths": ["/api/example"],
            "notes": ["Line one\nLine two\nLine three"],
        },
    )

    assert "  notes:" in text
    assert "    - Line one" in text
    assert "      Line two" in text
    assert "      Line three" in text


def test_render_help_includes_server_builtin(monkeypatch):
    monkeypatch.setattr(helpmod, "list_profiles", lambda: ["dev", "prod"])
    monkeypatch.setattr(helpmod, "profiles_env_path", lambda: "/tmp/profiles.env")
    monkeypatch.setattr(helpmod, "credentials_env_path", lambda: "/tmp/credentials.env")

    text = helpmod.render_help({}, {"module": "server"}, version="1.6.0")

    assert "netloom server use <profile>" in text
    assert "Configured profiles:" in text
    assert "  - dev" in text
    assert "  - prod" in text


def test_render_help_without_catalog_lists_builtin_modules():
    text = helpmod.render_help({}, {}, version="1.4.7")

    assert "Available modules:" in text
    assert "  - cache" in text
    assert "  - copy" in text
    assert "  - server" in text


def test_render_help_includes_copy_builtin():
    text = helpmod.render_help({}, {"module": "copy"}, version="1.6.0")

    assert "Built-in module: copy" in text
    assert "--from=SOURCE_PROFILE" in text
    assert "--on-conflict=fail|skip|update|replace" in text
    assert "copied across all matching paged results" in text


def test_render_help_includes_copy_as_service_action():
    text = helpmod.render_help(
        {
            "modules": {
                "policyelements": {
                    "network-device": {
                        "actions": {
                            "list": {"method": "GET", "paths": ["/api/network-device"]}
                        }
                    }
                }
            }
        },
        {"module": "policyelements", "service": "network-device"},
        version="1.7.1",
    )

    assert "Available actions:" in text
    assert "copy" in text


def test_render_help_for_copy_action():
    text = helpmod.render_help(
        {
            "modules": {
                "policyelements": {
                    "network-device": {
                        "actions": {
                            "list": {"method": "GET", "paths": ["/api/network-device"]}
                        }
                    }
                }
            }
        },
        {
            "module": "policyelements",
            "service": "network-device",
            "action": "copy",
        },
        version="1.7.1",
    )

    assert "usage: netloom <module> <service> copy" in text
    assert "legacy alias: netloom copy <module> <service>" in text


def test_render_help_mentions_filter_paging_behavior():
    text = helpmod.render_help({}, {}, version="1.6.0")

    assert "list/get --all keep paging until all matching rows are fetched" in text
    assert "fetches every matching page, not just the first 1000 results" in text


def test_render_help_includes_ascii_banner():
    text = helpmod.render_help({}, {}, version="1.6.2")

    assert "_   _      _   _" in text
    assert "netloom v1.6.2" in text
