package top.tqx.demo_1.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import top.tqx.demo_1.common.Result;
import top.tqx.demo_1.dto.DepartmentCreateRequest;
import top.tqx.demo_1.dto.DepartmentUpdateRequest;
import top.tqx.demo_1.service.DepartmentService;
import top.tqx.demo_1.vo.DepartmentVO;
import top.tqx.demo_1.vo.UserVO;

import java.util.List;

@Tag(name = "部门管理", description = "部门的增删改查操作")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    private final DepartmentService departmentService;

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

    @Operation(summary = "查询部门列表", description = "获取所有部门信息")
    @GetMapping
    public Result<List<DepartmentVO>> listDepartments() {
        List<DepartmentVO> departments = departmentService.listDepartments();
        return Result.success(departments);
    }

    @Operation(summary = "创建部门", description = "创建新部门")
    @PostMapping
    public Result<Void> createDepartment(@RequestBody DepartmentCreateRequest request) {
        departmentService.createDepartment(request);
        return Result.success();
    }

    @Operation(summary = "更新部门", description = "更新部门信息")
    @PutMapping("/{id}")
    public Result<Void> updateDepartment(
            @Parameter(description = "部门ID") @PathVariable Long id, 
            @RequestBody DepartmentUpdateRequest request) {
        departmentService.updateDepartment(id, request);
        return Result.success();
    }

    @Operation(summary = "删除部门", description = "根据ID删除部门")
    @DeleteMapping("/{id}")
    public Result<Void> deleteDepartment(@Parameter(description = "部门ID") @PathVariable Long id) {
        departmentService.deleteDepartment(id);
        return Result.success();
    }

    @Operation(summary = "设置部门管理员", description = "为部门设置管理员")
    @PutMapping("/{id}/manager")
    public Result<Void> setDepartmentManager(
            @Parameter(description = "部门ID") @PathVariable Long id,
            @Parameter(description = "管理员用户ID") @RequestParam Long managerId) {
        departmentService.setDepartmentManager(id, managerId);
        return Result.success();
    }

    @Operation(summary = "查询部门用户", description = "获取部门下的所有用户")
    @GetMapping("/{id}/users")
    public Result<List<UserVO>> getDepartmentUsers(
            @Parameter(description = "部门ID") @PathVariable Long id) {
        List<UserVO> users = departmentService.getDepartmentUsers(id);
        return Result.success(users);
    }
}