from playwright.sync_api import Page, expect


def test_profile_page(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()

    # Navigate to profile
    page.get_by_role("link", name="Profile").click()

    # Check profile sections
    expect(page.get_by_role("heading", name="Profile")).to_be_visible()
    expect(page.get_by_role("tab", name="Overview")).to_be_visible()
    expect(page.get_by_role("tab", name="Reviews")).to_be_visible()
    expect(page.get_by_role("tab", name="Photos")).to_be_visible()
    expect(page.get_by_role("tab", name="Settings")).to_be_visible()


def test_edit_profile(page: Page, test_images):
    # 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()

    # Navigate to profile settings
    page.get_by_role("link", name="Profile").click()
    page.get_by_role("tab", name="Settings").click()

    # Edit profile information
    page.get_by_label("Display Name").fill("Test User")
    page.get_by_label("Bio").fill("Theme park enthusiast")
    page.get_by_label("Location").fill("Orlando, FL")

    # Upload avatar
    page.get_by_label("Avatar").set_input_files(test_images["test_avatar"])

    # Save changes
    page.get_by_role("button", name="Save Changes").click()

    # Verify updates
    expect(page.get_by_text("Profile updated successfully")).to_be_visible()
    expect(page.get_by_text("Test User")).to_be_visible()
    expect(page.get_by_text("Theme park enthusiast")).to_be_visible()
    expect(page.get_by_text("Orlando, FL")).to_be_visible()


def test_change_password(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()

    # Navigate to profile settings
    page.get_by_role("link", name="Profile").click()
    page.get_by_role("tab", name="Settings").click()

    # Click change password
    page.get_by_role("link", name="Change Password").click()

    # Fill password form
    page.get_by_label("Current Password").fill("testpass123")
    page.get_by_label("New Password").fill("newpass123")
    page.get_by_label("Confirm New Password").fill("newpass123")

    # Submit form
    page.get_by_role("button", name="Change Password").click()

    # Verify password changed
    expect(page.get_by_text("Password changed successfully")).to_be_visible()


def test_email_preferences(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()

    # Navigate to profile settings
    page.get_by_role("link", name="Profile").click()
    page.get_by_role("tab", name="Settings").click()

    # Click email preferences
    page.get_by_role("link", name="Email Preferences").click()

    # Update preferences
    page.get_by_label("Newsletter").check()
    page.get_by_label("Review Notifications").check()
    page.get_by_label("Photo Comments").uncheck()

    # Save changes
    page.get_by_role("button", name="Save Preferences").click()

    # Verify updates
    expect(page.get_by_text("Email preferences updated")).to_be_visible()


def test_privacy_settings(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()

    # Navigate to profile settings
    page.get_by_role("link", name="Profile").click()
    page.get_by_role("tab", name="Settings").click()

    # Click privacy settings
    page.get_by_role("link", name="Privacy Settings").click()

    # Update settings
    page.get_by_label("Show Email").uncheck()
    page.get_by_label("Show Location").check()
    page.get_by_label("Public Profile").check()

    # Save changes
    page.get_by_role("button", name="Save Privacy Settings").click()

    # Verify updates
    expect(page.get_by_text("Privacy settings updated")).to_be_visible()


def test_connected_accounts(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()

    # Navigate to profile settings
    page.get_by_role("link", name="Profile").click()
    page.get_by_role("tab", name="Settings").click()

    # Click connected accounts
    page.get_by_role("link", name="Connected Accounts").click()

    # Check available connections
    expect(page.get_by_role("link", name="Connect Google")).to_be_visible()
    expect(page.get_by_role("link", name="Connect Discord")).to_be_visible()


def test_delete_account(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()

    # Navigate to profile settings
    page.get_by_role("link", name="Profile").click()
    page.get_by_role("tab", name="Settings").click()

    # Click delete account
    page.get_by_role("link", name="Delete Account").click()

    # Confirm deletion
    page.get_by_label("Password").fill("testpass123")
    page.get_by_label("Confirm").check()

    # Click delete button
    page.get_by_role("button", name="Delete Account").click()

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