from tests.conftest import BASE_URL


def test_messages_page_loads(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/messages")
    assert page.is_visible(".messages-layout")


def test_messages_search_input(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/messages")
    search = page.locator("#message-search")
    assert search.is_visible()
    assert search.get_attribute("placeholder") == "Search conversations..."


def test_messages_empty_state(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/messages", wait_until="domcontentloaded")
    assert page.is_visible(".messages-layout") or page.is_visible("text=No conversations yet")


def test_messages_search_for_bob(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/messages")
    page.fill("#message-search", "bob_test")
    page.locator("#message-search").press("Enter")
    page.wait_for_timeout(500)
    assert page.is_visible(".messages-layout")


def test_messages_header_visible(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/messages")
    assert page.is_visible("text=Home")
    assert page.is_visible("text=Projects")


def test_messages_bell_icon(alice):
    page, _ = alice
    page.goto(f"{BASE_URL}/messages")
    bell = page.locator(".topnav-icon").first
    assert bell.is_visible()


def test_messages_topnav_user(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/messages")
    assert page.is_visible(f"text={user['username']}")


def test_messages_avatar_on_page(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/messages")
    avatar = page.locator("img.avatar-img").first
    assert avatar.is_visible()
