"""Tenant Admin config for the Public Portal navigation menu:
PATCH /tenants/me/portal-menu (menus:edit, never a portal user) and its
read-back through GET /tenants/me."""


async def test_defaults_reproduce_the_original_look(client, auth_headers):
    resp = await client.get("/tenants/me", headers=auth_headers)
    assert resp.status_code == 200, resp.text
    s = resp.json()["portal_menu_settings"]
    assert s["position"] == "left" and s["item_style"] == "classic" and s["width"] == 256
    assert s["search_enabled"] is False and s["collapsible"] is True and s["tooltips"] == "collapsed"
    assert s["background_color"] is None


async def test_settings_round_trip(client, auth_headers):
    settings = {
        "position": "top",
        "item_style": "underline",
        "density": "compact",
        "width": 300,
        "show_icons": False,
        "background_color": "#0f172a",
        "active_background": "#1e293b",
        "active_text": "#38bdf8",
        "tooltips": "always",
        "search_enabled": True,
        "search_placeholder": "Find a screen",
        "collapsible": False,
        "collapsible_groups": True,
        "groups_default_collapsed": True,
    }
    resp = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json=settings)
    assert resp.status_code == 200, resp.text
    read = (await client.get("/tenants/me", headers=auth_headers)).json()["portal_menu_settings"]
    for key, value in settings.items():
        assert read[key] == value, key
    # untouched keys keep their defaults
    assert read["show_group_labels"] is True and read["text_color"] is None

    # reset to defaults
    reset = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={})
    assert reset.status_code == 200
    assert reset.json()["portal_menu_settings"]["position"] == "left"


async def test_invalid_values_are_rejected(client, auth_headers):
    for bad in ({"position": "bottom"}, {"item_style": "neon"}, {"background_color": "red"}, {"width": 50}, {"tooltips": "sometimes"}):
        resp = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json=bad)
        assert resp.status_code == 422, bad


async def _upload(client, headers, name, content_type, data=b"\x89PNG\r\n\x1a\n" + b"0" * 64):
    resp = await client.post("/uploads", headers=headers, files={"file": (name, data, content_type)})
    assert resp.status_code == 201, resp.text
    return resp.json()["id"]


async def test_logo_upload_round_trip_and_checks(client, auth_headers):
    logo = await _upload(client, auth_headers, "logo.png", "image/png")
    ok = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={"logo_file_id": logo, "logo_height": 40, "logo_show_title": False})
    assert ok.status_code == 200, ok.text
    saved = (await client.get("/tenants/me", headers=auth_headers)).json()["portal_menu_settings"]
    assert saved["logo_file_id"] == logo and saved["logo_height"] == 40 and saved["logo_show_title"] is False

    # not an image
    pdf = await _upload(client, auth_headers, "notes.pdf", "application/pdf")
    bad_type = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={"logo_file_id": pdf})
    assert bad_type.status_code == 422 and "PNG" in bad_type.json()["detail"]

    # too large (2 MB + 1)
    big = await _upload(client, auth_headers, "big.png", "image/png", data=b"0" * (2 * 1024 * 1024 + 1))
    too_big = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={"logo_file_id": big})
    assert too_big.status_code == 422 and "2 MB" in too_big.json()["detail"]

    # a file that doesn't exist
    missing = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={"logo_file_id": "11111111-1111-1111-1111-111111111111"})
    assert missing.status_code == 422

    for fid in (logo, pdf, big):
        await client.delete(f"/uploads/{fid}", headers=auth_headers)
    await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={})


async def test_logo_url_must_be_http_or_https(client, auth_headers):
    ok = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={"logo_url": "https://example.com/logo.svg"})
    assert ok.status_code == 200, ok.text
    for bad in ("javascript:alert(1)", "data:image/png;base64,AAAA", "ftp://example.com/a.png", "https://has space.com/a.png"):
        resp = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={"logo_url": bad})
        assert resp.status_code == 422, bad
    await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={})


async def test_logo_height_bounds(client, auth_headers):
    for bad in (10, 100):
        resp = await client.patch("/tenants/me/portal-menu", headers=auth_headers, json={"logo_height": bad})
        assert resp.status_code == 422, bad
