package com.zioinfo.hrm.uiws.system.controller;

import com.zioinfo.hrm.common.ApiResponse;
import com.zioinfo.hrm.uiws.system.dto.DeptUserRoleDto;
import com.zioinfo.hrm.uiws.system.dto.PageResponse;
import com.zioinfo.hrm.uiws.system.dto.RoleIdsRequest;
import com.zioinfo.hrm.uiws.system.service.DeptRoleService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/** 2.2 부서권한(dept-role) — 부서 사용자+권한 조회 / 부여 / 삭제. base: /api/system/depts/{deptId}/roles */
@RestController
@RequestMapping("/api/system/depts/{deptId}/roles")
@RequiredArgsConstructor
public class DeptRoleController {

    private final DeptRoleService deptRoleService;

    @GetMapping
    public ApiResponse<PageResponse<DeptUserRoleDto>> list(
            @PathVariable("deptId") String deptId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(deptRoleService.listDeptUsers(deptId, page, size));
    }

    @PostMapping
    public ApiResponse<Void> grant(@PathVariable("deptId") String deptId, @Valid @RequestBody RoleIdsRequest req) {
        deptRoleService.grant(deptId, req.roleIds());
        return ApiResponse.ok(null);
    }

    @DeleteMapping
    public ApiResponse<Void> revoke(@PathVariable("deptId") String deptId, @Valid @RequestBody RoleIdsRequest req) {
        deptRoleService.revoke(deptId, req.roleIds());
        return ApiResponse.ok(null);
    }
}
