import os

import pytest
from sqlalchemy import select

from govcrawler.db import get_sessionmaker
from govcrawler.models import Article, CrawlLog
from govcrawler.pipeline import fetch_and_store
from govcrawler.sites.gdqy import (
    COLUMN_ID,
    SITE_ID,
    TARGET_ARTICLE_KEY,
    TARGET_ARTICLE_URL,
)

RUN_NETWORK = os.environ.get("GOVCRAWLER_RUN_SMOKE") == "1"

pytestmark = pytest.mark.skipif(
    not RUN_NETWORK,
    reason="real-network smoke; set GOVCRAWLER_RUN_SMOKE=1 to run",
)


def test_smoke_gdqy_post_2136593():
    r = fetch_and_store(SITE_ID, COLUMN_ID, TARGET_ARTICLE_KEY, TARGET_ARTICLE_URL)
    assert r["status"] == "ready", f"unexpected: {r}"
    assert r["article_id"]

    Session = get_sessionmaker()
    with Session() as s:
        a = s.execute(select(Article).where(Article.id == r["article_id"])).scalar_one()
        assert a.status == "ready"
        assert a.url_hash and len(a.url_hash) == 64
        assert a.content_simhash and a.content_simhash != "0" * 16
        assert a.raw_html_path and a.text_path
        assert "无人驾驶航空器" in (a.content_text or "") or "无人机" in (a.content_text or "")
        cl = (
            s.execute(
                select(CrawlLog)
                .where(CrawlLog.article_url == TARGET_ARTICLE_URL)
                .order_by(CrawlLog.id.desc())
            )
            .scalars()
            .first()
        )
        assert cl is not None
        assert cl.success is True
        # strategy depends on Cookie pool state:
        #   cold run → playwright; warm run (same host within Cookie TTL) → httpx.
        # Both are acceptable outcomes; the assertion is 'we succeeded', not 'we
        # chose a specific tier'.
        assert cl.strategy in ("playwright", "httpx")
