"""
用户行为分析 API
记录和分析用户操作
"""

from fastapi import APIRouter, HTTPException, Depends, Header
from pydantic import BaseModel
from typing import Optional, Any
from datetime import datetime
from app.core.security import get_current_user
from app.core.config import settings

router = APIRouter()

# 简单的内存存储（生产环境应该用数据库）
action_logs = []

class ActionLog(BaseModel):
    action: str
    details: Optional[Any] = None
    timestamp: int
    session_id: str
    device_id: Optional[str] = None

class ActionLogResponse(BaseModel):
    success: bool
    message: str

@router.post("/log", response_model=ActionLogResponse)
async def log_action(log: ActionLog, current_username: str = Depends(get_current_user)):
    """记录用户行为（仅记录当前登录用户的操作）"""
    try:
        log_entry = {
            "username": current_username,
            "action": log.action,
            "details": log.details,
            "timestamp": log.timestamp,
            "session_id": log.session_id,
            "device_id": log.device_id,
            "server_time": datetime.now().isoformat(),
        }
        
        action_logs.append(log_entry)
        
        # 只保留最近 10000 条
        if len(action_logs) > 10000:
            action_logs.pop(0)
        
        return ActionLogResponse(success=True, message="已记录")
    except Exception as e:
        return ActionLogResponse(success=False, message=str(e))

@router.get("/stats/me")
async def get_my_stats(current_username: str = Depends(get_current_user)):
    """获取当前用户的统计信息"""
    user_logs = [log for log in action_logs if log.get("username") == current_username]
    
    action_counts: dict[str, int] = {}
    for log in user_logs:
        action = log.get("action", "unknown")
        action_counts[action] = action_counts.get(action, 0) + 1
    
    return {
        "username": current_username,
        "total_actions": len(user_logs),
        "action_counts": action_counts,
        "recent_actions": user_logs[-10:]
    }

@router.get("/recent")
async def get_recent_logs(
    limit: int = 100,
    admin_token: Optional[str] = Header(None, alias="X-Admin-Token")
):
    """获取最近的操作日志（仅管理员）"""
    if admin_token != settings.ADMIN_TOKEN:
        raise HTTPException(status_code=403, detail="需要管理员权限")
    
    return {
        "total": len(action_logs),
        "logs": action_logs[-limit:]
    }
