import sqlite3
import tempfile
import unittest
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo

from lib.hub.hub_symbol_archive_lib import init_db, list_archive_calendar, upsert_trades_cache, upsert_trade_overlay


def _bj_ms(y, m, d, hh, mm):
    dt = datetime(y, m, d, hh, mm, 0, tzinfo=ZoneInfo("Asia/Shanghai"))
    return int(dt.timestamp() * 1000)


class ArchiveCalendarTests(unittest.TestCase):
    def test_calendar_groups_by_trading_day_and_sick(self):
        with tempfile.TemporaryDirectory() as td:
            db = Path(td) / "arch.db"
            init_db(db)
            upsert_trades_cache(
                "binance",
                [
                    {
                        "id": 1,
                        "symbol": "BTC/USDT",
                        "direction": "long",
                        "result": "止盈",
                        "pnl_amount": 10.0,
                        "opened_at": "2026-06-18 09:00:00",
                        "closed_at": "2026-06-18 10:00:00",
                        "closed_at_ms": _bj_ms(2026, 6, 18, 10, 0),
                        "exchange_turnover_usdt": 2000.0,
                        "exchange_commission_usdt": 0.8,
                    },
                    {
                        "id": 2,
                        "symbol": "ETH/USDT",
                        "direction": "short",
                        "result": "止损",
                        "pnl_amount": -5.0,
                        "opened_at": "2026-06-18 14:00:00",
                        "closed_at": "2026-06-18 15:00:00",
                        "closed_at_ms": _bj_ms(2026, 6, 18, 15, 0),
                    },
                ],
                db_path=db,
            )
            upsert_trade_overlay("binance", 2, behavior_tag="sick", db_path=db)
            payload = list_archive_calendar(2026, 6, db_path=db)
            self.assertEqual(payload["month"], 6)
            days = payload["days"]
            self.assertTrue(days)
            sick_days = [d for d in days.values() if d.get("has_sick")]
            self.assertTrue(sick_days)
            self.assertGreaterEqual(payload["month_open_count"], 2)


if __name__ == "__main__":
    unittest.main()
