from tests.conftest import BASE_URL


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


def test_profile_stats(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    assert page.is_visible("text=Posts")
    assert page.is_visible("text=Level")
    assert page.is_visible("text=Stars")


def test_profile_level_bar(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    assert page.is_visible("text=Progress to next level")
    level_bar = page.locator(".bar")
    assert level_bar.is_visible()


def test_profile_badges(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    badges = page.locator(".profile-badge")
    assert badges.count() >= 1


def test_profile_tabs(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    tabs = ["Posts", "Projects", "Activity"]
    for tab in tabs:
        assert page.is_visible(f"a:has-text('{tab}')")


def test_profile_posts_tab(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}?tab=posts", wait_until="domcontentloaded")


def test_profile_projects_tab_content(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}?tab=projects", wait_until="domcontentloaded")


def test_profile_info_section(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    assert page.is_visible("text=Bio")
    assert page.is_visible("text=Location")


def test_profile_back_link(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    back = page.locator("a:has-text('Back')").first
    assert back.is_visible()
    back.click()
    page.wait_for_url(f"{BASE_URL}/feed", wait_until="domcontentloaded")


def test_profile_role_display(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    assert page.is_visible("text=Member")


def test_profile_avatar(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    avatar = page.locator(".avatar-lg")
    assert avatar.is_visible()


def test_profile_nav_from_topbar(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
    page.locator(".topnav-user").click()
    page.wait_for_timeout(300)
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    assert page.is_visible(f"text={user['username']}")


def test_profile_edit_bio(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    page.evaluate("""
        document.querySelector('textarea[name="bio"]').value = 'Test bio for testing';
        document.querySelector('form[action="/profile/update"]').submit();
    """)
    page.wait_for_timeout(500)
    assert page.is_visible("text=Test bio for testing")


def test_profile_edit_location(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    page.evaluate("""
        document.querySelector('input[name="location"]').value = 'Amsterdam';
        document.querySelector('form[action="/profile/update"]').submit();
    """)
    page.wait_for_timeout(500)
    assert page.is_visible("text=Amsterdam")


def test_profile_edit_all_fields(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    page.evaluate("""
        document.querySelector('textarea[name="bio"]').value = 'Full stack dev';
        document.querySelector('input[name="location"]').value = 'London, UK';
        document.querySelector('input[name="git_link"]').value = 'https://git.example.com/alice';
        document.querySelector('input[name="website"]').value = 'https://alice.example.com';
        document.querySelector('form[action="/profile/update"]').submit();
    """)
    page.wait_for_timeout(500)
    assert page.is_visible("text=Full stack dev")
    assert page.is_visible("text=London, UK")
    assert page.is_visible("text=https://git.example.com/alice")
    assert page.is_visible("text=https://alice.example.com")


def test_profile_viewing_other_user(bob, alice):
    page_b, user_b = bob
    page_b.goto(f"{BASE_URL}/profile/alice_test")
    assert page_b.is_visible("text=alice_test")


def test_profile_message_button(bob, alice):
    page_b, _ = bob
    page_b.goto(f"{BASE_URL}/profile/alice_test", wait_until="domcontentloaded")
    message_btn = page_b.locator("a:has-text('Send Message')")
    assert message_btn.is_visible()


def test_profile_message_button_self(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}", wait_until="domcontentloaded")
    message_btn = page.locator("a:has-text('Send Message')")
    assert not message_btn.is_visible()


def test_profile_activity_tab(alice):
    page, user = alice
    page.goto(f"{BASE_URL}/profile/{user['username']}?tab=activity", wait_until="domcontentloaded")
    page.wait_for_timeout(300)
