package com.gcsc.connection.common.controller;

import com.gcsc.connection.common.dto.ApiResponse;
import com.gcsc.connection.common.dto.SystemConfigResponse;
import com.gcsc.connection.common.dto.UpdateSystemConfigRequest;
import com.gcsc.connection.common.service.SystemConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 시스템 공통 설정 API
 */
@RestController
@RequestMapping("/api/config")
@RequiredArgsConstructor
public class SystemConfigController {

    private final SystemConfigService systemConfigService;

    /**
     * 설정 값 단건 조회
     */
    @GetMapping("/{configKey}")
    public ResponseEntity<ApiResponse<SystemConfigResponse>> getConfig(@PathVariable String configKey) {
        SystemConfigResponse response = systemConfigService.getConfigValue(configKey);
        return ResponseEntity.ok(ApiResponse.ok(response));
    }

    /**
     * 설정 값 저장 (upsert)
     */
    @PutMapping("/{configKey}")
    public ResponseEntity<ApiResponse<SystemConfigResponse>> updateConfig(
        @PathVariable String configKey,
        @RequestBody UpdateSystemConfigRequest request
    ) {
        SystemConfigResponse response = systemConfigService.updateConfig(configKey, request);
        return ResponseEntity.ok(ApiResponse.ok(response));
    }
}
