package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.AiConfigCallStatsRequest;
import com.emotion.dto.request.AdminCreateRequest;
import com.emotion.dto.request.AdminPageRequest;
import com.emotion.dto.request.AdminUpdateRequest;
import com.emotion.dto.response.AiConfigCallStatsResponse;
import com.emotion.dto.response.AdminResponse;
import com.emotion.dto.response.DashboardStatsResponse;
import com.emotion.service.AdminService;
import com.emotion.service.DashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 管理员控制器
 * 
 * @author huazhongmin
 * @date 2025-10-27
 */
@RestController
@RequestMapping("/admin")
@Tag(name = "管理员管理", description = "管理员的增删改查功能")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private DashboardService dashboardService;

    /**
     * 分页查询管理员
     */
    @Operation(summary = "分页查询管理员", description = "分页查询管理员列表")
    @GetMapping("/page")
    public Result<PageResult<AdminResponse>> getPage(@Validated AdminPageRequest request) {
        PageResult<AdminResponse> pageResult = adminService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取管理员
     */
    @Operation(summary = "根据ID获取管理员", description = "根据ID获取管理员详情")
    @GetMapping("/detail")
    public Result<AdminResponse> getById(@RequestParam String id) {
        AdminResponse response = adminService.getAdminResponseById(id);
        if (response == null) {
            return Result.notFound("管理员不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建管理员
     */
    @Operation(summary = "创建管理员", description = "创建新的管理员")
    @PostMapping("/create")
    public Result<AdminResponse> create(@RequestBody @Validated AdminCreateRequest request) {
        AdminResponse response = adminService.createAdminWithResponse(request);
        if (response == null) {
            return Result.error("创建失败");
        }
        return Result.success("创建成功", response);
    }

    /**
     * 更新管理员
     */
    @Operation(summary = "更新管理员", description = "更新指定管理员")
    @PutMapping("/update")
    public Result<AdminResponse> update(@RequestBody @Validated AdminUpdateRequest request) {
        AdminResponse response = adminService.updateAdminWithResponse(request);
        if (response == null) {
            return Result.error("更新失败");
        }
        return Result.success("更新成功", response);
    }

    /**
     * 删除管理员
     */
    @Operation(summary = "删除管理员", description = "删除指定管理员")
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = adminService.removeById(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功", null);
    }

    /**
     * 获取仪表盘统计数据
     */
    @Operation(summary = "获取仪表盘统计数据", description = "获取管理后台仪表盘的统计数据，包括用户、内容、AI服务和系统统计")
    @GetMapping("/dashboard/stats")
    public Result<DashboardStatsResponse> getDashboardStats() {
        DashboardStatsResponse stats = dashboardService.getDashboardStats();
        return Result.success("获取成功", stats);
    }

    /**
     * 获取用户统计数据
     */
    @Operation(summary = "获取用户统计数据", description = "获取用户相关的统计数据")
    @GetMapping("/dashboard/user-stats")
    public Result<DashboardStatsResponse.UserStats> getUserStats() {
        DashboardStatsResponse.UserStats userStats = dashboardService.getUserStats();
        return Result.success("获取成功", userStats);
    }

    /**
     * 获取内容统计数据
     */
    @Operation(summary = "获取内容统计数据", description = "获取内容相关的统计数据")
    @GetMapping("/dashboard/content-stats")
    public Result<DashboardStatsResponse.ContentStats> getContentStats() {
        DashboardStatsResponse.ContentStats contentStats = dashboardService.getContentStats();
        return Result.success("获取成功", contentStats);
    }

    /**
     * 获取AI服务统计数据
     */
    @Operation(summary = "获取AI服务统计数据", description = "获取AI服务相关的统计数据")
    @GetMapping("/dashboard/ai-stats")
    public Result<DashboardStatsResponse.AiServiceStats> getAiServiceStats() {
        DashboardStatsResponse.AiServiceStats aiStats = dashboardService.getAiServiceStats();
        return Result.success("获取成功", aiStats);
    }

    /**
     * 获取系统统计数据
     */
    @Operation(summary = "获取系统统计数据", description = "获取系统相关的统计数据")
    @GetMapping("/dashboard/system-stats")
    public Result<DashboardStatsResponse.SystemStats> getSystemStats() {
        DashboardStatsResponse.SystemStats systemStats = dashboardService.getSystemStats();
        return Result.success("获取成功", systemStats);
    }

    /**
     * 获取用户增长趋势数据
     */
    @Operation(summary = "获取用户增长趋势数据", description = "获取指定天数的用户增长趋势数据")
    @GetMapping("/dashboard/user-growth-trends")
    public Result<List<DashboardStatsResponse.UserGrowthTrend>> getUserGrowthTrends(
            @RequestParam(defaultValue = "7") int days) {
        List<DashboardStatsResponse.UserGrowthTrend> trends = dashboardService.getUserGrowthTrends(days);
        return Result.success("获取成功", trends);
    }

    /**
     * 获取最近登录用户
     */
    @Operation(summary = "获取最近登录用户", description = "获取最近登录的用户列表")
    @GetMapping("/dashboard/recent-logins")
    public Result<List<DashboardStatsResponse.RecentLogin>> getRecentLogins(
            @RequestParam(defaultValue = "10") int limit) {
        List<DashboardStatsResponse.RecentLogin> recentLogins = dashboardService.getRecentLogins(limit);
        return Result.success("获取成功", recentLogins);
    }

    /**
     * 获取 AI 配置调用次数统计
     */
    @Operation(summary = "获取AI配置调用次数统计", description = "按 t_ai_config 的 workflow_id 关联 t_coze_api_call 统计调用次数并按次数倒序返回")
    @GetMapping(value = "/dashboard/aiConfigCallStats")
    public Result<AiConfigCallStatsResponse> getAiConfigCallStats(@Validated AiConfigCallStatsRequest request) {
        AiConfigCallStatsResponse response = dashboardService.getAiConfigCallStats(request);
        return Result.success("获取成功", response);
    }
}
