import datetime as dt
from unittest.mock import AsyncMock, patch
from fastapi.testclient import TestClient

from app.main import app


def test_today_kst_uses_kst_offset_not_utc(monkeypatch):
    """컨테이너가 UTC(Alpine, tzdata 미설치)라 date.today()는 08시 KST에 어제를 준다.
    _today_kst()는 UTC+9로 보정해 오늘(KST)을 반환해야 한다."""
    from app.screener import router

    class _FrozenDT(dt.datetime):
        @classmethod
        def utcnow(cls):
            # 2026-07-01 23:30 UTC == 2026-07-02 08:30 KST (AI 뉴스 리포트 시각대)
            return dt.datetime(2026, 7, 1, 23, 30, 0)

    monkeypatch.setattr(router.dt, "datetime", _FrozenDT)
    assert router._today_kst() == dt.date(2026, 7, 2)


def test_refresh_news_sentiment_weekend_skip():
    # 2026-05-16 = Saturday
    client = TestClient(app)
    resp = client.post(
        "/api/stock/screener/snapshot/refresh-news-sentiment?asof=2026-05-16"
    )
    assert resp.status_code == 200
    assert resp.json()["status"] == "skipped_weekend"


def test_refresh_news_sentiment_weekday_invokes_pipeline():
    fake_summary = {
        "asof": "2026-05-13", "updated": 3, "failures": [],
        "duration_sec": 1.0, "tokens_input": 100, "tokens_output": 20,
        "top_pos": [], "top_neg": [], "model": "m",
        "mapping": {"total_articles": 5, "matched_pairs": 8, "hit_tickers": 3},
    }
    with patch("app.screener.router._ai_pipeline") as mp, \
         patch("app.screener.router._ai_telegram") as mt:
        mp.refresh_daily = AsyncMock(return_value=fake_summary)
        mt.build_message = lambda **kw: f"TEXT_with_mapping={kw.get('mapping')}"
        client = TestClient(app)
        resp = client.post(
            "/api/stock/screener/snapshot/refresh-news-sentiment?asof=2026-05-13"
        )
    assert resp.status_code == 200
    body = resp.json()
    assert body["mapping"]["hit_tickers"] == 3
    assert "mapping=" in body["telegram_text"]
