package com.luojia_channel.modules.admin.controller;

import com.luojia_channel.common.domain.Result;
import com.luojia_channel.common.domain.page.PageResponse;
import com.luojia_channel.modules.admin.dto.*;
import com.luojia_channel.modules.admin.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin")
@RequiredArgsConstructor
@Tag(name = "管理员模块", description = "管理员相关接口")
public class AdminController {

    private final AdminService adminService;

    @GetMapping("/overview")
    @Operation(summary = "获取系统总览数据", description = "获取系统各项统计数据，包括用户总数、帖子总数、评论总数、活跃用户等")
    public Result<AdminOverviewDTO> getOverview() {
        return Result.success(adminService.getOverview());
    }

    @GetMapping("/users")
    @Operation(summary = "获取用户列表", description = "分页获取用户列表，支持按用户名、手机号、邮箱等搜索筛选")
    public Result<PageResponse<AdminUserDTO>> getUsers(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "用户角色") @RequestParam(required = false) Integer role,
            @Parameter(description = "用户状态") @RequestParam(required = false) Integer status
    ) {
        return Result.success(adminService.getUserList(page, size, keyword, role, status));
    }

    @PutMapping("/users/{id}/status")
    @Operation(summary = "更改用户状态", description = "冻结或解冻用户")
    public Result<Void> changeUserStatus(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "状态（1正常 2冻结）") @RequestParam Integer status
    ) {
        adminService.changeUserStatus(id, status);
        return Result.success();
    }

    @PutMapping("/users/{id}/role")
    @Operation(summary = "更改用户角色", description = "升级或降级用户角色")
    public Result<Void> changeUserRole(
            @Parameter(description = "用户ID") @PathVariable Long id,
            @Parameter(description = "角色（1普通用户 2管理员 3超级管理员）") @RequestParam Integer role
    ) {
        adminService.changeUserRole(id, role);
        return Result.success();
    }

    @GetMapping("/posts")
    @Operation(summary = "获取帖子列表", description = "分页获取帖子列表，支持按标题、内容、分类等搜索筛选")
    public Result<PageResponse<AdminPostDTO>> getPosts(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "分类ID") @RequestParam(required = false) Long categoryId,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status
    ) {
        return Result.success(adminService.getPostList(page, size, keyword, categoryId, status));
    }

    @DeleteMapping("/posts/{id}")
    @Operation(summary = "删除帖子", description = "管理员删除帖子")
    public Result<Void> deletePost(@Parameter(description = "帖子ID") @PathVariable Long id) {
        adminService.deletePost(id);
        return Result.success();
    }

    @PutMapping("/posts/{id}/status")
    @Operation(summary = "更改帖子状态", description = "置顶、取消置顶、隐藏或显示帖子")
    public Result<Void> changePostStatus(
            @Parameter(description = "帖子ID") @PathVariable Long id,
            @Parameter(description = "操作类型(1置顶 2取消置顶 3隐藏 4显示)") @RequestParam Integer action
    ) {
        adminService.changePostStatus(id, action);
        return Result.success();
    }

    @GetMapping("/comments")
    @Operation(summary = "获取评论列表", description = "分页获取评论列表，支持按内容、帖子ID等搜索筛选")
    public Result<PageResponse<AdminCommentDTO>> getComments(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "帖子ID") @RequestParam(required = false) Long postId
    ) {
        return Result.success(adminService.getCommentList(page, size, keyword, postId));
    }

    @DeleteMapping("/comments/{id}")
    @Operation(summary = "删除评论", description = "管理员删除评论")
    public Result<Void> deleteComment(@Parameter(description = "评论ID") @PathVariable Long id) {
        adminService.deleteComment(id);
        return Result.success();
    }

    @GetMapping("/statistics/user")
    @Operation(summary = "获取用户统计数据", description = "获取用户注册增长、活跃度等统计数据")
    public Result<Map<String, Object>> getUserStatistics(
            @Parameter(description = "统计类型(daily,weekly,monthly)") @RequestParam(defaultValue = "daily") String type,
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate
    ) {
        return Result.success(adminService.getUserStatistics(type, startDate, endDate));
    }

    @GetMapping("/statistics/post")
    @Operation(summary = "获取帖子统计数据", description = "获取帖子发布、点赞、收藏等统计数据")
    public Result<Map<String, Object>> getPostStatistics(
            @Parameter(description = "统计类型(daily,weekly,monthly)") @RequestParam(defaultValue = "daily") String type,
            @Parameter(description = "开始日期") @RequestParam(required = false) String startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) String endDate
    ) {
        return Result.success(adminService.getPostStatistics(type, startDate, endDate));
    }
} 