import os
import tempfile

import pytest
from fastapi.testclient import TestClient

from app import db as db_module


@pytest.fixture
def client(monkeypatch):
    fd, path = tempfile.mkstemp(suffix=".db")
    os.close(fd)
    monkeypatch.setattr(db_module, "DB_PATH", path)
    db_module.init_db()
    from app import main
    monkeypatch.setattr(main, "DB_PATH", path)
    with TestClient(main.app) as c:
        yield c
    import gc
    gc.collect()
    for ext in ("", "-wal", "-shm"):
        try:
            os.remove(path + ext)
        except OSError:
            pass


def test_health(client):
    resp = client.get("/health")
    assert resp.status_code == 200
    assert resp.json()["ok"] is True


def test_status_endpoint(client):
    resp = client.get("/api/insta/status")
    assert resp.status_code == 200
    j = resp.json()
    assert "naver_api" in j and "anthropic_api" in j


def test_news_articles_listing(client):
    db_module.add_news_article({
        "category": "economy", "title": "T1", "link": "https://x/1", "summary": "S",
    })
    resp = client.get("/api/insta/news/articles?category=economy&days=7")
    assert resp.status_code == 200
    assert len(resp.json()["items"]) == 1


def test_keywords_listing(client):
    db_module.add_trending_keyword({
        "keyword": "K", "category": "economy", "score": 0.5, "articles_count": 3,
    })
    resp = client.get("/api/insta/keywords?category=economy")
    assert resp.status_code == 200
    assert resp.json()["items"][0]["keyword"] == "K"


def test_create_slate_kicks_background_task(client, monkeypatch):
    """Plan-B-Insta SP-4: 슬레이트 생성 후 Redis push → task status=processing (Windows worker 대기).

    card_renderer는 NAS에서 제거됨. write_slate → Redis rpush 경로만 검증.
    """
    from app import main, card_writer

    def fake_write(keyword, category, articles=None):
        return db_module.add_card_slate({
            "keyword": keyword, "category": category, "status": "draft",
            "cover_copy": {"headline": "H", "body": "B", "accent_color": "#000"},
            "body_copies": [{"headline": f"h{i}", "body": f"b{i}"} for i in range(8)],
            "cta_copy": {"headline": "C", "body": "B", "cta": "F"},
        })

    async def fake_rpush(queue, payload):
        pass  # Redis 없이도 테스트 통과

    monkeypatch.setattr(card_writer, "write_slate", fake_write)
    monkeypatch.setattr(main.redis_client, "rpush", fake_rpush)

    resp = client.post("/api/insta/slates", json={"keyword": "K", "category": "economy"})
    assert resp.status_code == 200
    task_id = resp.json()["task_id"]
    # 잠시 대기 후 폴링 — background task가 완료될 때까지
    import time
    for _ in range(20):
        st = client.get(f"/api/insta/tasks/{task_id}").json()
        if st["status"] != "pending":
            break
        time.sleep(0.1)
    # Redis push 후 task는 processing 상태 (Windows worker가 rendered로 전환)
    assert st["status"] == "processing"
    assert st["result_id"] is not None  # slate_id가 result_id에 기록됨
    slate_id = st["result_id"]
    detail = client.get(f"/api/insta/slates/{slate_id}").json()
    assert detail["keyword"] == "K"
