package com.qqchen.deploy.backend.system.api;

import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.system.model.query.OnlineUserQuery;
import com.qqchen.deploy.backend.system.model.vo.OnlineStatisticsVO;
import com.qqchen.deploy.backend.system.model.vo.OnlineUserVO;
import com.qqchen.deploy.backend.system.service.IOnlineUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 在线用户管理接口
 * 
 * @author qqchen
 * @date 2025-11-20
 */
@Tag(name = "在线用户管理")
@RestController
@RequestMapping("/api/v1/online")
public class OnlineUserController {
    
    @Resource
    private IOnlineUserService onlineUserService;
    
    /**
     * 获取在线用户列表（分页）
     */
    @Operation(summary = "获取在线用户列表")
    @GetMapping("/users")
    @PreAuthorize("hasAuthority('system:online:view')")
    public Response<Page<OnlineUserVO>> getOnlineUsers(OnlineUserQuery query) {
        Page<OnlineUserVO> result = onlineUserService.getOnlineUsers(query);
        return Response.success(result);
    }
    
    /**
     * 获取在线用户统计
     */
    @Operation(summary = "获取在线统计信息")
    @GetMapping("/statistics")
    public Response<OnlineStatisticsVO> getStatistics() {
        OnlineStatisticsVO statistics = onlineUserService.getStatistics();
        return Response.success(statistics);
    }
    
    /**
     * 强制用户下线
     */
    @Operation(summary = "强制用户下线")
    @DeleteMapping("/kick/{userId}")
    @PreAuthorize("hasAuthority('system:online:kick')")
    public Response<Void> kickUser(
        @Parameter(description = "用户ID", required = true) @PathVariable Long userId
    ) {
        onlineUserService.kickUser(userId);
        return Response.success();
    }
}
