package com.agileboot.system.config.controller;

import com.agileboot.common.core.core.R;
import com.agileboot.common.mybatis.core.page.PageR;
import com.agileboot.system.config.dto.ConfigQuery;
import com.agileboot.system.config.dto.ConfigUpdate;
import com.agileboot.system.config.service.ISysConfigService;
import com.agileboot.system.config.vo.ConfigVO;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author cuiJiaWang
 * @Create 2025-08-13 11:49
 */
@RestController
@RequestMapping("/system")
@Validated
@RequiredArgsConstructor
public class SysConfigController {

    private final ISysConfigService sysConfigService;

    @GetMapping("/test")
    @Deprecated
    public R<String> test(@RequestParam("key") String key) {
        return R.ok(sysConfigService.getConfigValueByKey(key));
    }

    /**
     * 获取参数配置列表
     */
    @GetMapping("/configs")
    public PageR<ConfigVO> list(ConfigQuery query) {
        return sysConfigService.getConfigPage(query);
    }

    /**
     * 根据参数编号获取详细信息
     */
    @GetMapping(value = "/config/{configId}")
    public R<ConfigVO> getInfo(@NotNull @Positive @PathVariable Long configId) {
        return R.ok(sysConfigService.getConfigInfo(configId));
    }

    /**
     * 修改参数配置
     */
    @PostMapping(value = "/config/{id}")
    public R<Void> edit(@NotNull @Positive @PathVariable Long id, @RequestBody ConfigUpdate config) {
        sysConfigService.updateConfig(id, config);
        return R.ok();
    }
}
