import pytest
from uuid import uuid4
from datetime import datetime
from tests.conftest import BASE_URL
from devplacepy.database import get_table
from devplacepy.utils import make_combined_slug


def test_landing_page_loads(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    assert "DevPlace" in page.title()
    assert page.is_visible("text=Devplace.net")
    assert page.is_visible("text=The Developer Social Network")


def test_landing_tagline(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    tagline = page.text_content("p")
    assert "Track industry shifts" in tagline


def test_landing_join_button(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    btn = page.locator("text=Join DevPlace Free")
    assert btn.is_visible()
    assert btn.get_attribute("href") == "/auth/signup"


def test_landing_features(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    features = [
        "100% Free Forever",
        "Zero Ads",
        "No Censorship",
        "No Payments",
    ]
    for feature in features:
        assert page.is_visible(f"text={feature}")


def test_landing_nav_links(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    assert page.is_visible("a:has-text('Home')")
    assert page.is_visible("a:has-text('News')")
    assert page.is_visible("a:has-text('Projects')")
    assert page.is_visible("a:has-text('Login')")
    assert page.is_visible("a:has-text('Sign Up')")


def test_landing_nav_to_login(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    page.click("a:has-text('Login')")
    page.wait_for_url(f"{BASE_URL}/auth/login", wait_until="domcontentloaded")
    assert page.is_visible("h2:has-text('Welcome Back')")


def test_landing_nav_to_signup(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    page.click("a:has-text('Sign Up')")
    page.wait_for_url(f"{BASE_URL}/auth/signup", wait_until="domcontentloaded")
    assert page.is_visible("h2:has-text('Join the Community')")


def test_landing_log_in_link(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    page.click("text=Log In")
    page.wait_for_url(f"{BASE_URL}/auth/login", wait_until="domcontentloaded")


def test_landing_authenticated_redirects_to_feed(page, app_server):
    page.goto(f"{BASE_URL}/auth/signup", wait_until="domcontentloaded")
    page.fill("#username", "redirect_user")
    page.fill("#email", "redirect@test.devplace")
    page.fill("#password", "secret123")
    page.fill("#confirm_password", "secret123")
    with page.expect_navigation(timeout=10000):
        page.click("button:has-text('Create account')")
    page.wait_for_url("**/feed", timeout=10000, wait_until="domcontentloaded")

    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    page.wait_for_url("**/feed", timeout=10000, wait_until="domcontentloaded")


def test_landing_footer_links(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    assert page.is_visible("a:has-text('Bug Report')")


def test_landing_hero_present(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    assert page.is_visible("text=Log In")
    assert page.is_visible("text=Join DevPlace Free")
    assert page.is_visible("text=Devplace.net")


def test_landing_news_section(page, app_server):
    page.goto(f"{BASE_URL}/", wait_until="domcontentloaded")
    loc = page.locator("text=Developer News")
    try:
        loc.wait_for(state="visible", timeout=3000)
    except Exception:
        pytest.skip("No news articles available (news service disabled)")
    cards = page.locator(".news-card-title a")
    assert cards.count() > 0, "No news articles visible on landing page"
