package com.haha.admin.controller;

import com.haha.admin.annotation.RequirePermission;
import com.haha.common.annotation.Log;
import com.haha.common.enums.OperationType;
import com.haha.common.vo.Result;
import com.haha.entity.DataPermission;
import com.haha.service.DataPermissionService;
import com.haha.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 数据权限管理控制器
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/permission")
public class DataPermissionController {

    private final DataPermissionService dataPermissionService;

    private final RoleService roleService;

    /**
     * 获取所有权限列表
     */
    @RequirePermission("role:read")
    @GetMapping("/list")
    public Result<List<DataPermission>> getAllPermissions() {
        return dataPermissionService.getAllPermissions();
    }

    /**
     * 获取所有权限(按模块分组)
     */
    @RequirePermission("role:read")
    @GetMapping("/list-grouped")
    public Result<List<Map<String, Object>>> getPermissionsGroupedByModule() {
        return dataPermissionService.getPermissionsTree();
    }

    /**
     * 根据模块编码获取权限列表
     */
    @RequirePermission("role:read")
    @GetMapping("/module/{moduleCode}")
    public Result<List<DataPermission>> getPermissionsByModule(@PathVariable String moduleCode) {
        return dataPermissionService.getPermissionsByModule(moduleCode);
    }

    /**
     * 根据角色ID获取权限ID列表
     */
    @RequirePermission("role:read")
    @GetMapping("/role/{roleId}")
    public Result<List<Long>> getRolePermissionIds(@PathVariable Long roleId) {
        return roleService.getRolePermissionIds(roleId);
    }

    /**
     * 更新角色权限
     */
    @RequirePermission("role:update")
    @Log(module = "权限管理", operation = OperationType.UPDATE, summary = "更新角色权限")
    @PostMapping("/role/{roleId}")
    public Result<Void> updateRolePermissions(
            @PathVariable Long roleId,
            @RequestBody List<Long> permissionIds) {
        return roleService.updateRolePermissions(roleId, permissionIds);
    }

    /**
     * 创建权限定义
     */
    @RequirePermission("role:create")
    @Log(module = "权限管理", operation = OperationType.INSERT, summary = "创建权限定义")
    @PostMapping("/create")
    public Result<Void> createPermission(@RequestBody DataPermission permission) {
        return dataPermissionService.createPermission(permission);
    }

    /**
     * 更新权限定义
     */
    @RequirePermission("role:update")
    @Log(module = "权限管理", operation = OperationType.UPDATE, summary = "更新权限定义")
    @PutMapping("/update")
    public Result<Void> updatePermission(@RequestBody DataPermission permission) {
        return dataPermissionService.updatePermission(permission);
    }

    /**
     * 删除权限定义
     */
    @RequirePermission("role:delete")
    @Log(module = "权限管理", operation = OperationType.DELETE, summary = "删除权限定义")
    @DeleteMapping("/{id}")
    public Result<Void> deletePermission(@PathVariable Long id) {
        return dataPermissionService.deletePermission(id);
    }

    /**
     * 切换权限状态
     */
    @RequirePermission("role:update")
    @Log(module = "权限管理", operation = OperationType.UPDATE, summary = "切换权限状态")
    @PutMapping("/{id}/status")
    public Result<Void> togglePermissionStatus(
            @PathVariable Long id,
            @RequestBody Integer status) {
        return dataPermissionService.togglePermissionStatus(id, status);
    }
}
