# retoor <retoor@molodetz.nl>

import time
import requests
from tests.conftest import BASE_URL
from devplacepy.database import get_table
AJAX_comments = {"X-Requested-With": "fetch"}
_counter_comments = [0]
def _session_comments():
    _counter_comments[0] += 1
    name = f"cmt{int(time.time() * 1000)}{_counter_comments[0]}"
    s = requests.Session()
    s.post(
        f"{BASE_URL}/auth/signup",
        data={
            "username": name,
            "email": f"{name}@t.dev",
            "password": "secret123",
            "confirm_password": "secret123",
            "birth_date": "1990-01-01",
            "accept_terms": "1",
        },
        allow_redirects=True,
    )
    return s, name
def _create_post_comments(session, title):
    r = session.post(
        f"{BASE_URL}/posts/create",
        data={
            "content": "Post for comment test.",
            "title": title,
            "topic": "devlog",
        },
        allow_redirects=False,
    )
    slug = r.headers["location"].split("/posts/")[-1]
    return get_table("posts").find_one(slug=slug)["uid"]
def _seed_news_seo():
    from datetime import datetime, timezone
    from devplacepy.database import get_table
    from devplacepy.utils import generate_uid, make_combined_slug

    uid = generate_uid()
    title = f"SEO Test News Article {uid.split('-')[-1]}"
    slug = make_combined_slug(title, uid)
    get_table("news").insert(
        {
            "deleted_at": None,
            "deleted_by": None,
            "uid": uid,
            "slug": slug,
            "title": title,
            "description": "A seeded news article for SEO tests.",
            "content": "Body content for the seeded article.",
            "url": "https://example.com/article",
            "source_name": "ExampleSource",
            "status": "published",
            "synced_at": datetime.now(timezone.utc).isoformat(),
            "show_on_landing": 0,
            "grade": 8,
        }
    )
    return slug, uid
def _seed_owner():
    from datetime import datetime, timezone
    from uuid import uuid4
    from devplacepy.database import get_table

    uid = str(uuid4())
    get_table("users").insert(
        {
            "uid": uid,
            "username": f"seo_{uid[:8]}",
            "terms_version": "1",
            "email": f"{uid[:8]}@seo.test",
            "password_hash": "x",
            "role": "Member",
            "is_active": True,
            "created_at": datetime.now(timezone.utc).isoformat(),
        }
    )
    return uid
def _seed_post_seo(image=None):
    from datetime import datetime, timezone
    from uuid import uuid4
    from devplacepy.database import get_table
    from devplacepy.utils import make_combined_slug

    owner = _seed_owner()
    uid = str(uuid4())
    slug = make_combined_slug("SEO Detail Post", uid)
    get_table("posts").insert(
        {
            "deleted_at": None,
            "deleted_by": None,
            "uid": uid,
            "user_uid": owner,
            "slug": slug,
            "title": "SEO Detail Post",
            "content": "Body text for the SEO detail post.",
            "topic": "general",
            "project_uid": None,
            "image": image,
            "stars": 0,
            "created_at": datetime.now(timezone.utc).isoformat(),
        }
    )
    return slug, uid
def _seed_gist():
    from datetime import datetime, timezone
    from uuid import uuid4
    from devplacepy.database import get_table
    from devplacepy.utils import make_combined_slug

    owner = _seed_owner()
    uid = str(uuid4())
    slug = make_combined_slug("SEO Detail Gist", uid)
    get_table("gists").insert(
        {
            "deleted_at": None,
            "deleted_by": None,
            "uid": uid,
            "user_uid": owner,
            "slug": slug,
            "title": "SEO Detail Gist",
            "description": "Gist description for SEO tests.",
            "source_code": "print('seo')",
            "language": "python",
            "stars": 0,
            "created_at": datetime.now(timezone.utc).isoformat(),
        }
    )
    return slug, uid
def _seed_project():
    from datetime import datetime, timezone
    from uuid import uuid4
    from devplacepy.database import get_table
    from devplacepy.utils import make_combined_slug

    owner = _seed_owner()
    uid = str(uuid4())
    slug = make_combined_slug("SEO Detail Project", uid)
    get_table("projects").insert(
        {
            "deleted_at": None,
            "deleted_by": None,
            "uid": uid,
            "user_uid": owner,
            "slug": slug,
            "title": "SEO Detail Project",
            "description": "Project description for SEO tests.",
            "project_type": "software",
            "platforms": "Linux",
            "status": "Released",
            "stars": 0,
            "created_at": datetime.now(timezone.utc).isoformat(),
        }
    )
    return slug, uid
def _seed_news_image(news_uid):
    from devplacepy.database import get_table

    get_table("news_images").insert(
        {
            "deleted_at": None,
            "deleted_by": None,
            "news_uid": news_uid,
            "url": "https://example.com/seo-news-image.jpg",
        }
    )
def _seed_feed_posts(count):
    from datetime import datetime, timezone, timedelta
    from uuid import uuid4
    from devplacepy.database import get_table
    from devplacepy.utils import make_combined_slug

    owner = _seed_owner()
    topic = f"seopag{owner[:8]}"
    base = datetime(2026, 1, 1, tzinfo=timezone.utc)
    posts = get_table("posts")
    for i in range(count):
        uid = str(uuid4())
        posts.insert(
            {
                "deleted_at": None,
                "deleted_by": None,
                "uid": uid,
                "user_uid": owner,
                "slug": make_combined_slug(f"seo pag {i}", uid),
                "title": None,
                "content": f"seo pag post {i}",
                "topic": topic,
                "project_uid": None,
                "image": None,
                "stars": 0,
                "created_at": (base - timedelta(seconds=i)).isoformat(),
            }
        )
    return topic


def test_comment_form_submission_ui(alice, app_server):
    page, user = alice
    title = f"ui-comment-{int(time.time() * 1000)}"
    s = requests.Session()
    s.post(
        f"{BASE_URL}/auth/login",
        data={"email": user["email"], "password": user["password"]},
    )
    r = s.post(
        f"{BASE_URL}/posts/create",
        data={"content": "UI comment target.", "title": title, "topic": "devlog"},
        allow_redirects=False,
    )
    slug = r.headers["location"].split("/posts/")[-1]
    page.goto(f"{BASE_URL}/posts/{slug}", wait_until="domcontentloaded")
    textarea = page.locator("form.comment-form textarea[name='content']").first
    textarea.wait_for(state="visible", timeout=5000)
    textarea.fill("UI test comment")
    page.locator("button.comment-form-submit").first.click()
    page.wait_for_timeout(1000)
    assert page.is_visible("text=UI test comment")


def test_post_og_image_uses_content_image(page, app_server):
    slug, _ = _seed_post_seo(image="seo-content.png")
    page.goto(f"{BASE_URL}/posts/{slug}", wait_until="domcontentloaded")
    og = page.locator('meta[property="og:image"]').get_attribute("content")
    assert og.endswith("/static/uploads/seo-content.png"), og


def test_post_page_has_breadcrumb_schema(page, app_server):
    slug, _ = _seed_post_seo()
    page.goto(f"{BASE_URL}/posts/{slug}", wait_until="domcontentloaded")
    scripts = page.locator('script[type="application/ld+json"]')
    text = " ".join(scripts.nth(i).text_content() for i in range(scripts.count()))
    assert "BreadcrumbList" in text


def test_post_schema_has_date_modified(page, app_server):
    slug, _ = _seed_post_seo()
    page.goto(f"{BASE_URL}/posts/{slug}", wait_until="domcontentloaded")
    scripts = page.locator('script[type="application/ld+json"]')
    text = " ".join(scripts.nth(i).text_content() for i in range(scripts.count()))
    assert "dateModified" in text


def test_post_detail_has_no_rel_next(page, app_server):
    slug, _ = _seed_post_seo()
    page.goto(f"{BASE_URL}/posts/{slug}", wait_until="domcontentloaded")
    assert page.locator('link[rel="next"]').count() == 0
