package com.mosquito.project.permission;

import com.mosquito.project.dto.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller - 部门CRUD API
 */
@RestController
@RequestMapping("/api/v1/departments")
public class DepartmentController {

    private final DepartmentService departmentService;

    public DepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    // 权限码常量 (使用PRD四段式格式)
    private static final String PERM_DEPARTMENT_VIEW = "department.index.view.ALL";
    private static final String PERM_DEPARTMENT_MANAGE = "department.index.manage.ALL";

    /**
     * 获取部门列表
     */
    @GetMapping
    @RequirePermission(PERM_DEPARTMENT_VIEW)
    public ApiResponse<List<SysDepartment>> list() {
        return ApiResponse.success(departmentService.findAll());
    }

    /**
     * 获取顶级部门列表
     */
    @GetMapping("/roots")
    @RequirePermission(PERM_DEPARTMENT_VIEW)
    public ApiResponse<List<SysDepartment>> roots() {
        return ApiResponse.success(departmentService.findRootDepartments());
    }

    /**
     * 根据父部门ID获取子部门
     */
    @GetMapping("/children/{parentId}")
    @RequirePermission(PERM_DEPARTMENT_VIEW)
    public ApiResponse<List<SysDepartment>> children(@PathVariable Long parentId) {
        return ApiResponse.success(departmentService.findByParentId(parentId));
    }

    /**
     * 根据ID获取部门
     */
    @GetMapping("/{id}")
    @RequirePermission(PERM_DEPARTMENT_VIEW)
    public ApiResponse<SysDepartment> getById(@PathVariable Long id) {
        return departmentService.findById(id)
                .map(dept -> ApiResponse.success(dept))
                .orElse(ApiResponse.error(404, "部门不存在"));
    }

    /**
     * 创建部门
     */
    @PostMapping
    @RequirePermission(PERM_DEPARTMENT_MANAGE)
    public ApiResponse<SysDepartment> create(@RequestBody SysDepartment department) {
        try {
            SysDepartment saved = departmentService.save(department);
            return ApiResponse.success(saved, "部门创建成功");
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(400, e.getMessage());
        }
    }

    /**
     * 更新部门
     */
    @PutMapping("/{id}")
    @RequirePermission(PERM_DEPARTMENT_MANAGE)
    public ApiResponse<SysDepartment> update(@PathVariable Long id, @RequestBody SysDepartment department) {
        return departmentService.update(id, department)
                .map(updated -> ApiResponse.success(updated, "部门更新成功"))
                .orElse(ApiResponse.error(404, "部门不存在"));
    }

    /**
     * 删除部门
     */
    @DeleteMapping("/{id}")
    @RequirePermission(PERM_DEPARTMENT_MANAGE)
    public ApiResponse<Void> delete(@PathVariable Long id) {
        try {
            departmentService.delete(id);
            return ApiResponse.success(null, "部门删除成功");
        } catch (IllegalArgumentException e) {
            return ApiResponse.error(400, e.getMessage());
        }
    }
}
