package com.zioinfo.kintex.system.infraconfig;

import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.common.audit.Audited;
import com.zioinfo.kintex.system.SystemAccessGuard;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 시스템관리 — 알림 채널 설정 조회/변경 API. ADMIN 게이트. 채널 on/off. */
@RestController
@RequestMapping("/api/admin/notify-config")
public class NotifyConfigController {

    private static final String PREFIX = "notify.";
    private final InfraConfigService service;
    private final SystemAccessGuard guard;

    public NotifyConfigController(InfraConfigService service, SystemAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    @GetMapping
    public ApiResponse<List<InfraConfigDto>> list(@AuthenticationPrincipal KintexPrincipal principal) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.listByPrefix(PREFIX));
    }

    @Audited(action = "NOTIFY_CONFIG_SAVE", targetType = "sys_setting")
    @PutMapping
    public ApiResponse<Void> update(@AuthenticationPrincipal KintexPrincipal principal,
                                    @RequestBody InfraConfigUpdateRequest req) {
        guard.requireAdmin(principal);
        service.update(PREFIX, req, principal.userId());
        return ApiResponse.ok(null);
    }
}
