from typing import Any, List
from datetime import datetime
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session

from app.api.v1 import deps
from app.models.user import User, UserRole
from app.schemas.operation_log import OperationLogList, ActionType
from app.services.log_service import LogService

router = APIRouter()

@router.get("/", response_model=OperationLogList, summary="查询操作日志")
def read_logs(
    skip: int = 0,
    limit: int = 20,
    app_id: int = None, # Optional, filter by app
    action_type: ActionType = None,
    operator_id: int = None,
    keyword: str = None, # Search target mobile
    start_date: datetime = None,
    end_date: datetime = None,
    db: Session = Depends(deps.get_db),
    current_user: User = Depends(deps.get_current_active_user),
):
    """
    查询操作日志。
    只有超级管理员可以查看所有日志。
    """
    if current_user.role != UserRole.SUPER_ADMIN:
        raise HTTPException(status_code=403, detail="权限不足")
        
    total, items = LogService.get_logs(
        db=db,
        skip=skip,
        limit=limit,
        app_id=app_id,
        action_type=action_type,
        operator_id=operator_id,
        keyword=keyword,
        start_date=start_date,
        end_date=end_date
    )
    
    return {"total": total, "items": items}

