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/account-codes")
@RequiredArgsConstructor
public class AccountCodeController {

    private final AccountCodeService service;

    /** 활성 계정코드 목록 — 드롭다운/배치 참조용. accountType 파라미터로 DEBIT/CREDIT/BOTH 필터 가능 */
    @GetMapping
    public ApiResponse<List<AccountCodeVO>> getActiveCodes(
            @RequestParam(required = false) String accountType) {
        return ApiResponse.ok(service.getActiveCodes(accountType));
    }

    // ----- 시스템관리 -----

    @GetMapping("/all")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "READ")
    public ApiResponse<List<AccountCodeVO>> listAll(
            @RequestParam(required = false) String accountType) {
        return ApiResponse.ok(service.listAll(accountType));
    }

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

    @PutMapping("/{accountCode}")
    @RequirePermission(menu = "SYSTEM_CODE", perm = "UPDATE")
    public ApiResponse<Void> update(@PathVariable String accountCode, @RequestBody AccountCodeVO vo) {
        vo.setAccountCode(accountCode);
        service.update(vo);
        return ApiResponse.ok();
    }

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