package com.zioinfo.mes.admin;

import com.zioinfo.mes.admin.dto.MesSetting;
import com.zioinfo.mes.admin.mapper.SettingMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class SettingService {

    private final SettingMapper mapper;
    private final AuditService auditService;

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

    public MesSetting update(String key, String value) {
        if (key == null || key.isBlank()) {
            throw new IllegalArgumentException("ERR-SET-400: key 필수");
        }
        mapper.upsert(key, value);
        auditService.log("SETTING_UPDATE", key, "value=" + value);
        return mapper.findByKey(key);
    }

    /** 설정값 조회(없으면 기본값). 다른 서비스의 임계값 참조용. */
    public String get(String key, String defaultValue) {
        MesSetting s = mapper.findByKey(key);
        return (s == null || s.getValue() == null) ? defaultValue : s.getValue();
    }
}
