import logging
import time
from typing import Any, Dict, Optional, Tuple
from fastapi import APIRouter, Query, Body, Path
from tortoise.expressions import Q

from app.controllers.token import token_controller
from app.schemas.base import Success, SuccessExtra
from app.schemas.token import BossTokenUpdate, BossTokenCreate

logger = logging.getLogger(__name__)

token_router = APIRouter()

# 简单内存缓存：key 为查询参数组合，value 为 (缓存时间戳, 响应数据)
_TOKENS_CACHE: Dict[Tuple[Any, Any, int, int], Tuple[float, Dict[str, Any]]] = {}
_CACHE_TTL_SECONDS: int = 60


@token_router.get("/tokens", summary="获取Boss Token列表")
async def list_boss_tokens(
    page: int = Query(1, description="页码"),
    page_size: int = Query(10, description="每页数量"),
    status: int = Query(None, description="状态筛选"),
    wt2: Optional[str] = Query(None),
    mpt: Optional[str] = Query(None),
):
    """获取Boss Token列表（带缓存）"""
    cache_key: Tuple[Any, Any, int, int] = (wt2, mpt, page, page_size)
    now = time.monotonic()
    cached = _TOKENS_CACHE.get(cache_key)
    if cached and (now - cached[0] < _CACHE_TTL_SECONDS):
        return cached[1]

    q = Q()
    if status is not None:
        q &= Q(status=status)
    if wt2:
        q &= Q(wt2__icontains=wt2)
    if mpt:
        q &= Q(mpt__icontains=mpt)

    total, token_objs = await token_controller.get_tokens(page=page, page_size=page_size, search=q)
    data = [await obj.to_dict() for obj in token_objs]

    resp = SuccessExtra(data=data, total=total, page=page, page_size=page_size)
    _TOKENS_CACHE[cache_key] = (now, resp)
    return resp


@token_router.get("/tokens/{token_id}", summary="获取Boss Token详情")
async def get_boss_token(
    token_id: int = Path(..., description="Token ID"),
):
    """获取Boss Token详情"""
    token_obj = await token_controller.get_token(token_id)
    token_dict = await token_obj.to_dict()
    return Success(data=token_dict)


@token_router.post("/tokens", summary="创建Boss Token")
async def create_boss_token(
    token_data: BossTokenCreate = Body(..., description="Token数据"),
):
    """创建Boss Token"""
    await token_controller.create_token(token_data)
    _TOKENS_CACHE.clear()
    return Success(msg="创建成功")


@token_router.put("/tokens/{token_id}", summary="更新Boss Token")
async def update_boss_token(
    token_id: int = Path(..., description="Token ID"),
    token_data: BossTokenUpdate = Body(..., description="Token数据"),
):
    """更新Boss Token"""
    await token_controller.update_token(token_id, token_data)
    _TOKENS_CACHE.clear()
    return Success(msg="更新成功")


@token_router.delete("/tokens/{token_id}", summary="删除Boss Token")
async def delete_boss_token(
    token_id: int = Path(..., description="Token ID"),
):
    """删除Boss Token"""
    await token_controller.delete_token(token_id)
    _TOKENS_CACHE.clear()
    return Success(msg="删除成功")


@token_router.post("/tokens/cache/clear", summary="强制清除Token缓存")
async def clear_token_cache():
    """强制清除Token列表缓存"""
    cache_size = len(_TOKENS_CACHE)
    _TOKENS_CACHE.clear()
    logger.info(f"手动清除Token缓存，清除了 {cache_size} 条缓存数据")
    return Success(msg=f"成功清除 {cache_size} 条Token缓存")
