from fastapi.testclient import TestClient

from src.main import app


def test_daily_report_preview_exposes_source_health_no_send_preview():
    client = TestClient(app)
    response = client.get("/api/v1/stats/daily/preview")

    assert response.status_code == 200
    data = response.json()
    assert "report_date" in data
    assert "alert_total" in data
    assert "km_new_entries" in data
    assert "playbook_count" in data
    assert "source_ok_count" in data
    assert "source_total_count" in data
    assert "source_confidence_percent" in data
    assert "source_gap_ids" in data
    assert "formatted_preview" in data

    preview = data["formatted_preview"]
    assert "AWOOOI 日度巡檢報告" in preview
    assert "報表資料源 / 沉澱" in preview
    assert f"來源: <code>{data['source_ok_count']}/{data['source_total_count']}</code>" in preview
    assert "不自動改排程" in preview


def test_weekly_report_preview_exposes_source_health_no_send_preview():
    client = TestClient(app)
    response = client.get("/api/v1/stats/weekly/preview")

    assert response.status_code == 200
    data = response.json()
    assert "week_range" in data
    assert "alert_total" in data
    assert "source_ok_count" in data
    assert "source_total_count" in data
    assert "source_confidence_percent" in data
    assert "source_gap_ids" in data
    assert "formatted_preview" in data
    assert data["source_total_count"] >= 0
    assert data["source_ok_count"] >= 0

    preview = data["formatted_preview"]
    assert "報表資料信任度" in preview
    if data["source_total_count"] > 0:
        assert "報表資料源 / 沉澱" in preview
        assert f"來源: <code>{data['source_ok_count']}/{data['source_total_count']}</code>" in preview
        assert "不自動改排程" in preview


def test_monthly_report_preview_exposes_source_health_and_gateway_delivery():
    client = TestClient(app)
    response = client.get("/api/v1/stats/monthly/preview")

    assert response.status_code == 200
    data = response.json()
    assert "report_month" in data
    assert "source_ok_count" in data
    assert "source_total_count" in data
    assert "source_confidence_percent" in data
    assert "source_gap_ids" in data
    assert "no_send_preview_count" in data
    assert "formatted_preview" in data

    preview = data["formatted_preview"]
    assert "AWOOOI 月報" in preview
    assert "報表資料源 / 沉澱" in preview
    assert f"來源: <code>{data['source_ok_count']}/{data['source_total_count']}</code>" in preview
    assert "Telegram Gateway" in preview
    assert "AI Agent 受控接手" in preview


def test_sre_digest_preview_exposes_source_health_no_send_preview():
    client = TestClient(app)
    response = client.get("/api/v1/stats/sre-digest/preview")

    assert response.status_code == 200
    data = response.json()
    assert "report_date" in data
    assert "source_ok_count" in data
    assert "source_total_count" in data
    assert "source_confidence_percent" in data
    assert "source_gap_ids" in data
    assert "no_send_preview_count" in data
    assert "live_send_allowed_count" in data
    assert "runtime_gate_count" in data
    assert "formatted_preview" in data
    assert data["live_send_allowed_count"] == 0
    assert data["runtime_gate_count"] == 0

    preview = data["formatted_preview"]
    assert "AwoooI SRE 戰情室 digest no-send preview" in preview
    assert "報表資料源 / 沉澱" in preview
    assert f"來源: <code>{data['source_ok_count']}/{data['source_total_count']}</code>" in preview
    assert "live Telegram send: 0" in preview
    assert "Gateway queue write: 0" in preview
    assert "不發 Telegram" in preview
    assert "不啟動 runtime gate" in preview
