package com.ga.common.code;

import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "공통코드")
@RestController
@RequestMapping("/api/common/codes")
@RequiredArgsConstructor
public class CommonCodeController {

    private final CommonCodeService service;

    /** 활성 코드 목록 조회 (드롭다운 용) — 인증된 사용자라면 모두 호출 가능 */
    @GetMapping("/{groupCode}")
    public ApiResponse<List<CommonCodeVO>> getCodes(@PathVariable String groupCode) {
        return ApiResponse.ok(service.getCodesByGroup(groupCode));
    }

    // ----- 시스템관리 화면 -----

    @GetMapping("/groups")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "READ")
    public ApiResponse<List<CommonCodeGroupVO>> listGroups(@RequestParam(required = false) String keyword) {
        return ApiResponse.ok(service.listGroups(keyword));
    }

    @PostMapping("/groups")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "CREATE")
    public ApiResponse<Void> createGroup(@RequestBody CommonCodeGroupVO vo) {
        service.createGroup(vo);
        return ApiResponse.ok();
    }

    @PutMapping("/groups/{groupCode}")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "UPDATE")
    public ApiResponse<Void> updateGroup(@PathVariable String groupCode, @RequestBody CommonCodeGroupVO vo) {
        vo.setGroupCode(groupCode);
        service.updateGroup(vo);
        return ApiResponse.ok();
    }

    @DeleteMapping("/groups/{groupCode}")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "DELETE")
    public ApiResponse<Void> deleteGroup(@PathVariable String groupCode) {
        service.deleteGroup(groupCode);
        return ApiResponse.ok();
    }

    @GetMapping("/groups/{groupCode}/codes")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "READ")
    public ApiResponse<List<CommonCodeVO>> listCodes(@PathVariable String groupCode) {
        return ApiResponse.ok(service.listCodes(groupCode));
    }

    @PostMapping("/groups/{groupCode}/codes")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "CREATE")
    public ApiResponse<Void> createCode(@PathVariable String groupCode, @RequestBody CommonCodeVO vo) {
        vo.setGroupCode(groupCode);
        service.createCode(vo);
        return ApiResponse.ok();
    }

    @PutMapping("/codes/{codeId}")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "UPDATE")
    public ApiResponse<Void> updateCode(@PathVariable Long codeId, @RequestBody CommonCodeVO vo) {
        vo.setCodeId(codeId);
        service.updateCode(vo);
        return ApiResponse.ok();
    }

    @DeleteMapping("/codes/{codeId}")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "DELETE")
    public ApiResponse<Void> deleteCode(@PathVariable Long codeId) {
        service.deleteCode(codeId);
        return ApiResponse.ok();
    }

    @PostMapping("/refresh")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "UPDATE")
    public ApiResponse<Void> refresh(@RequestParam(required = false) String groupCode) {
        if (groupCode == null || groupCode.isBlank()) service.refreshAll();
        else service.refreshCache(groupCode);
        return ApiResponse.ok();
    }
}
