package com.zioinfo.cms.admin;

import com.zioinfo.cms.admin.dto.CmsSetting;
import com.zioinfo.cms.admin.mapper.SettingMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/** 시스템 설정 서비스. 조회 SUPERADMIN/EDITOR, 변경 SUPERADMIN. */
@Service
@RequiredArgsConstructor
public class SettingService {

    private final SettingMapper mapper;
    private final AuditService auditService;

    public List<CmsSetting> list() {
        return mapper.findAll();
    }

    public CmsSetting update(String key, String value) {
        if (key == null || key.isBlank()) {
            throw new IllegalArgumentException("ERR-SET-400: key 필수");
        }
        if (value == null) {
            throw new IllegalArgumentException("ERR-SET-400: value 필수");
        }
        CmsSetting before = mapper.findByKey(key);
        mapper.upsert(key, value);
        String prev = before == null ? "(none)" : before.getValue();
        auditService.log("SETTING_CHANGE", key, prev + " -> " + value);
        return mapper.findByKey(key);
    }
}
