package com.mosquito.project.permission;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理控制器
 */
@RestController
@RequestMapping("/api/permissions")
public class PermissionController {

    private final PermissionService permissionService;
    private final PermissionCheckService permissionCheckService;

    public PermissionController(PermissionService permissionService, PermissionCheckService permissionCheckService) {
        this.permissionService = permissionService;
        this.permissionCheckService = permissionCheckService;
    }

    /**
     * 获取所有权限列表
     */
    @GetMapping
    public ResponseEntity<List<SysPermission>> getAllPermissions() {
        return ResponseEntity.ok(permissionService.findAll());
    }

    /**
     * 根据模块获取权限
     */
    @GetMapping("/module/{moduleCode}")
    public ResponseEntity<List<SysPermission>> getPermissionsByModule(@PathVariable String moduleCode) {
        return ResponseEntity.ok(permissionService.findByModuleCode(moduleCode));
    }

    /**
     * 获取当前用户权限信息
     */
    @GetMapping("/current")
    public ResponseEntity<CurrentUserPermissions> getCurrentUserPermissions() {
        Long userId = 1L;
        var permissions = new java.util.ArrayList<>(permissionCheckService.getUserPermissions(userId));
        var dataScope = permissionCheckService.getUserDataScope(userId);

        CurrentUserPermissions result = new CurrentUserPermissions();
        result.setUserId(userId);
        result.setPermissions(permissions);
        result.setDataScope(dataScope);
        result.setRoles(List.of());

        return ResponseEntity.ok(result);
    }

    /**
     * 检查用户是否拥有指定权限
     */
    @GetMapping("/check")
    public ResponseEntity<Boolean> checkPermission(@RequestParam String permissionCode) {
        Long userId = 1L;
        boolean hasPermission = permissionCheckService.hasPermission(userId, permissionCode);
        return ResponseEntity.ok(hasPermission);
    }

    /**
     * 检查用户是否拥有指定角色
     */
    @GetMapping("/role")
    public ResponseEntity<Boolean> checkRole(@RequestParam String roleCode) {
        Long userId = 1L;
        boolean hasRole = permissionCheckService.hasRole(userId, roleCode);
        return ResponseEntity.ok(hasRole);
    }

    /**
     * 获取用户数据权限范围
     */
    @GetMapping("/datascope")
    public ResponseEntity<String> getDataScope() {
        Long userId = 1L;
        String dataScope = permissionCheckService.getUserDataScope(userId);
        return ResponseEntity.ok(dataScope);
    }

    /**
     * 创建权限
     */
    @PostMapping
    public ResponseEntity<Long> createPermission(@RequestBody SysPermission permission) {
        SysPermission saved = permissionService.save(permission);
        return ResponseEntity.ok(saved.getId());
    }

    /**
     * 更新权限
     */
    @PutMapping("/{id}")
    public ResponseEntity<Void> updatePermission(@PathVariable Long id, @RequestBody SysPermission permission) {
        permission.setId(id);
        permissionService.update(id, permission);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePermission(@PathVariable Long id) {
        permissionService.delete(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 当前用户权限
     */
    public static class CurrentUserPermissions {
        private Long userId;
        private List<String> roles;
        private List<String> permissions;
        private String dataScope;

        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        public List<String> getRoles() { return roles; }
        public void setRoles(List<String> roles) { this.roles = roles; }
        public List<String> getPermissions() { return permissions; }
        public void setPermissions(List<String> permissions) { this.permissions = permissions; }
        public String getDataScope() { return dataScope; }
        public void setDataScope(String dataScope) { this.dataScope = dataScope; }
    }
}
