"""A portal role's own theme (Portal Roles › Theme) over the tenant's."""

import uuid


async def test_portal_roles_take_a_theme(client, auth_headers):
    role = (await client.post("/roles", headers=auth_headers, json={"name": f"Themed {uuid.uuid4().hex[:6]}", "scope": "portal"})).json()
    theme = {"brand_color": "#0f766e", "label_color": "#334155", "input_border_color": "#94a3b8", "background_color": "#f8fafc"}
    saved = await client.patch(f"/roles/{role['id']}", headers=auth_headers, json={"theme": theme})
    assert saved.status_code == 200, saved.text
    assert {k: saved.json()["theme"][k] for k in theme} == theme

    listed = next(r for r in (await client.get("/roles", headers=auth_headers, params={"scope": "portal"})).json() if r["id"] == role["id"])
    assert listed["theme"]["label_color"] == "#334155"

    bad = await client.patch(f"/roles/{role['id']}", headers=auth_headers, json={"theme": {"label_color": "slate"}})
    assert bad.status_code == 422

    cleared = await client.patch(f"/roles/{role['id']}", headers=auth_headers, json={"theme": None})
    assert cleared.json()["theme"] is None


async def test_internal_roles_follow_the_tenant_theme(client, auth_headers):
    role = (await client.post("/roles", headers=auth_headers, json={"name": f"Staff {uuid.uuid4().hex[:6]}", "scope": "internal"})).json()
    resp = await client.patch(f"/roles/{role['id']}", headers=auth_headers, json={"theme": {"brand_color": "#0f766e"}})
    assert resp.status_code == 422


async def test_portal_roles_take_their_own_menu_settings(client, auth_headers):
    role = (await client.post("/roles", headers=auth_headers, json={"name": f"Menu {uuid.uuid4().hex[:6]}", "scope": "portal"})).json()
    # Only what differs from the tenant's is stored, checked like the tenant's.
    saved = await client.patch(f"/roles/{role['id']}", headers=auth_headers, json={"menu_settings": {"position": "top", "active_background": "#0f766e"}})
    assert saved.status_code == 200, saved.text
    assert saved.json()["menu_settings"] == {"position": "top", "active_background": "#0f766e"}
    assert (await client.patch(f"/roles/{role['id']}", headers=auth_headers, json={"menu_settings": {"position": "middle"}})).status_code == 422
    assert (await client.patch(f"/roles/{role['id']}", headers=auth_headers, json={"menu_settings": {"sparkles": True}})).status_code == 422
    assert (await client.patch(f"/roles/{role['id']}", headers=auth_headers, json={"menu_settings": {"logo_file_id": str(uuid.uuid4())}})).status_code == 422
    cleared = await client.patch(f"/roles/{role['id']}", headers=auth_headers, json={"menu_settings": {}})
    assert cleared.json()["menu_settings"] is None
    internal = (await client.post("/roles", headers=auth_headers, json={"name": f"Staff {uuid.uuid4().hex[:6]}", "scope": "internal"})).json()
    assert (await client.patch(f"/roles/{internal['id']}", headers=auth_headers, json={"menu_settings": {"position": "top"}})).status_code == 422
