package com.ruoyi.cai.controller.admin;

import com.ruoyi.cai.dto.admin.SystemConfigResponse;
import com.ruoyi.cai.enums.SystemConfigEnum;
import com.ruoyi.cai.manager.SystemConfigManager;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cai/systemConfig")
public class SystemConfigController {
    @Autowired
    private SystemConfigManager systemConfigManager;
    @GetMapping("/all")
    public R<List<SystemConfigResponse>> all(){
        List<SystemConfigResponse> responses = new ArrayList<>();
        Map<String, String> allSystemConfig = systemConfigManager.getAllSystemConfig();
        SystemConfigEnum[] values = SystemConfigEnum.values();
        for (SystemConfigEnum value : values) {
            SystemConfigResponse sys = new SystemConfigResponse();
            sys.setKey(value.getKey());
            sys.setValue(allSystemConfig.getOrDefault(value.getKey(),value.getDefaultValue()));
            sys.setDesc(value.getDesc());
            responses.add(sys);
        }
        return R.ok(responses);
    }

    @GetMapping("/update")
    public R<Boolean> update(String key,String value){
        systemConfigManager.set(key,value);
        return R.ok(true);
    }
}
