package com.haha.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haha.admin.annotation.RequirePermission;
import com.haha.entity.dto.AdminQueryDTO;
import com.haha.entity.dto.ResetPasswordDTO;
import com.haha.common.vo.AdminStatisticsVO;
import com.haha.common.vo.PageResult;
import com.haha.common.vo.Result;
import com.haha.entity.Admin;
import com.haha.service.AdminService;
import com.haha.common.annotation.Log;
import com.haha.common.enums.OperationType;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员管理控制器
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class AdminController {

    private final AdminService adminService;
    
    /**
     * 分页查询管理员列表
     */
    @RequirePermission("admin:read")
    @PostMapping
    public Result<PageResult<Admin>> list(@RequestBody AdminQueryDTO queryDTO) {
        queryDTO.validate();
        
        Result<IPage<Admin>> result = adminService.getAdminList(
                queryDTO.getPage(),
                queryDTO.getPageSize(),
                queryDTO.getUsername(),
                queryDTO.getPhone(),
                queryDTO.getRoleId(),
                queryDTO.getStatus()
        );
        
        if (result.getCode() == 200 && result.getData() != null) {
            return Result.success("查询成功", PageResult.of(result.getData()));
        }
        return Result.error(result.getCode(), "查询失败");
    }
    
    /**
     * 获取管理员详情
     */
    @RequirePermission("admin:read")
    @GetMapping("/{id}")
    public Result<Admin> getById(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        if (admin == null) {
            return Result.error(404, "管理员不存在");
        }
        // 清除密码
        admin.setPassword(null);
        return Result.success("查询成功", admin);
    }
    
    /**
     * 添加管理员
     */
    @RequirePermission("admin:create")
    @Log(module = "用户管理", operation = OperationType.INSERT, summary = "添加管理员")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody Admin admin) {
        return adminService.addAdmin(admin);
    }
    
    /**
     * 更新管理员
     */
    @RequirePermission("admin:update")
    @Log(module = "用户管理", operation = OperationType.UPDATE, summary = "更新管理员")
    @PostMapping("/update")
    public Result<Void> update(@RequestBody Admin admin) {
        return adminService.updateAdmin(admin);
    }
    
    /**
     * 删除管理员
     */
    @RequirePermission("admin:delete")
    @Log(module = "用户管理", operation = OperationType.DELETE, summary = "删除管理员")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        return adminService.deleteAdmin(id);
    }
    
    /**
     * 重置密码
     */
    @RequirePermission("admin:update")
    @Log(module = "用户管理", operation = OperationType.UPDATE, summary = "重置管理员密码")
    @PostMapping("/reset-password")
    public Result<Void> resetPassword(@RequestBody ResetPasswordDTO dto) {
        return adminService.resetPassword(dto.getId(), dto.getNewPassword());
    }
    
    /**
     * 获取统计数据
     */
    @RequirePermission("admin:read")
    @GetMapping("/statistics")
    public Result<AdminStatisticsVO> statistics() {
        return adminService.getStatistics();
    }
}
