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

import com.zioinfo.hrm.common.ApiResponse;
import com.zioinfo.hrm.uiws.system.dto.CheckIdResponse;
import com.zioinfo.hrm.uiws.system.dto.IdsRequest;
import com.zioinfo.hrm.uiws.system.dto.PageResponse;
import com.zioinfo.hrm.uiws.system.dto.UserDto;
import com.zioinfo.hrm.uiws.system.dto.UserSaveDto;
import com.zioinfo.hrm.uiws.system.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 2.6 사용자(users) — 목록/검색/중복ID확인/CRUD/다중삭제/비번초기화/잠금해제/승인. base: /api/system/users */
@RestController
@RequestMapping("/api/system/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping
    public ApiResponse<PageResponse<UserDto>> list(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String deptId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(userService.list(keyword, deptId, page, size));
    }

    @GetMapping("/search")
    public ApiResponse<List<UserDto>> search(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String deptId) {
        return ApiResponse.ok(userService.searchPopup(keyword, deptId));
    }

    @GetMapping("/check-id")
    public ApiResponse<CheckIdResponse> checkId(@RequestParam String userId) {
        return ApiResponse.ok(userService.checkId(userId));
    }

    @PostMapping
    public ApiResponse<UserDto> create(@Valid @RequestBody UserSaveDto dto) {
        return ApiResponse.ok(userService.create(dto));
    }

    @GetMapping("/{id}")
    public ApiResponse<UserDto> get(@PathVariable("id") String id) {
        return ApiResponse.ok(userService.get(id));
    }

    @PutMapping("/{id}")
    public ApiResponse<UserDto> update(@PathVariable("id") String id, @Valid @RequestBody UserSaveDto dto) {
        return ApiResponse.ok(userService.update(id, dto));
    }

    @DeleteMapping
    public ApiResponse<Void> delete(@Valid @RequestBody IdsRequest req) {
        userService.delete(req.ids());
        return ApiResponse.ok(null);
    }

    @PostMapping("/{id}/reset-pw")
    public ApiResponse<Void> resetPassword(@PathVariable("id") String id) {
        userService.resetPassword(id);
        return ApiResponse.ok(null);
    }

    @PostMapping("/{id}/unlock")
    public ApiResponse<Void> unlock(@PathVariable("id") String id) {
        userService.unlock(id);
        return ApiResponse.ok(null);
    }

    @PostMapping("/{id}/approve")
    public ApiResponse<Void> approve(@PathVariable("id") String id) {
        userService.approve(id);
        return ApiResponse.ok(null);
    }

    @PostMapping("/{id}/revoke-approval")
    public ApiResponse<Void> revokeApproval(@PathVariable("id") String id) {
        userService.revokeApproval(id);
        return ApiResponse.ok(null);
    }
}
