package com.haha.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.haha.admin.annotation.RequirePermission;
import com.haha.service.AdminService;
import com.haha.common.annotation.Log;
import com.haha.common.enums.OperationType;
import com.haha.common.vo.Result;
import com.haha.entity.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 管理员管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class AdminController {
    
    @Autowired
    private AdminService adminService;
    
    /**
     * 分页查询管理员列表
     */
    @RequirePermission("admin:read")
    @PostMapping
    public Result<Map<String, Object>> list(@RequestBody Map<String, Object> params) {
        int page = Integer.parseInt(params.getOrDefault("page", 1).toString());
        int pageSize = Integer.parseInt(params.getOrDefault("pageSize", 10).toString());
        String username = (String) params.get("username");
        String phone = (String) params.get("phone");
        Long roleId = params.get("roleId") != null ? Long.valueOf(params.get("roleId").toString()) : null;
        Integer status = params.get("status") != null ? Integer.valueOf(params.get("status").toString()) : null;
        
        Result<IPage<Admin>> result = adminService.getAdminList(page, pageSize, username, phone, roleId, status);
        
        // 转换为前端需要的格式
        if (result.getCode() == 200 && result.getData() != null) {
            IPage<Admin> pageData = result.getData();
            Map<String, Object> data = Map.of(
                "list", pageData.getRecords(),
                "total", pageData.getTotal(),
                "pageSize", pageData.getSize(),
                "currentPage", pageData.getCurrent()
            );
            return Result.success("查询成功", data);
        }
        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 Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        String newPassword = (String) params.get("newPassword");
        return adminService.resetPassword(id, newPassword);
    }
    
    /**
     * 获取统计数据
     */
    @RequirePermission("admin:read")
    @GetMapping("/statistics")
    public Result<Map<String, Object>> statistics() {
        return adminService.getStatistics();
    }
}
