from playwright.sync_api import Page, expect


def test_login_page(page: Page):
    # Navigate to login page
    page.goto("http://localhost:8000/accounts/login/")

    # Check login form elements
    expect(page.get_by_label("Username")).to_be_visible()
    expect(page.get_by_label("Password")).to_be_visible()
    expect(page.get_by_role("button", name="Sign In")).to_be_visible()

    # Check social login buttons
    expect(page.get_by_role("link", name="Sign in with Google")).to_be_visible()
    expect(page.get_by_role("link", name="Sign in with Discord")).to_be_visible()


def test_successful_login(page: Page):
    # Navigate to login page
    page.goto("http://localhost:8000/accounts/login/")

    # Fill in login form
    page.get_by_label("Username").fill("testuser")
    page.get_by_label("Password").fill("testpass123")

    # Click login button
    page.get_by_role("button", name="Sign In").click()

    # Verify redirect to home page
    expect(page).to_have_url("http://localhost:8000/")
    expect(page.get_by_role("link", name="Profile")).to_be_visible()


def test_failed_login(page: Page):
    # Navigate to login page
    page.goto("http://localhost:8000/accounts/login/")

    # Fill in incorrect credentials
    page.get_by_label("Username").fill("wronguser")
    page.get_by_label("Password").fill("wrongpass")

    # Click login button
    page.get_by_role("button", name="Sign In").click()

    # Verify error message
    expect(page.get_by_text("Invalid username or password")).to_be_visible()


def test_signup_page(page: Page):
    # Navigate to signup page
    page.goto("http://localhost:8000/accounts/signup/")

    # Check signup form elements
    expect(page.get_by_label("Username")).to_be_visible()
    expect(page.get_by_label("Email")).to_be_visible()
    expect(page.get_by_label("Password")).to_be_visible()
    expect(page.get_by_label("Password (again)")).to_be_visible()
    expect(page.get_by_role("button", name="Sign Up")).to_be_visible()


def test_successful_signup(page: Page):
    # Navigate to signup page
    page.goto("http://localhost:8000/accounts/signup/")

    # Fill in signup form
    page.get_by_label("Username").fill("newuser")
    page.get_by_label("Email").fill("newuser@example.com")
    page.get_by_label("Password").fill("newpass123")
    page.get_by_label("Password (again)").fill("newpass123")

    # Click signup button
    page.get_by_role("button", name="Sign Up").click()

    # Verify redirect to home page
    expect(page).to_have_url("http://localhost:8000/")
    expect(page.get_by_role("link", name="Profile")).to_be_visible()


def test_logout(page: Page):
    # First login
    page.goto("http://localhost:8000/accounts/login/")
    page.get_by_label("Username").fill("testuser")
    page.get_by_label("Password").fill("testpass123")
    page.get_by_role("button", name="Sign In").click()

    # Click logout
    page.get_by_role("link", name="Logout").click()

    # Verify redirect to home page and logged out state
    expect(page).to_have_url("http://localhost:8000/")
    expect(page.get_by_role("link", name="Login")).to_be_visible()
