package com.jeesharp.business.modules.mall.controller;

import com.jeesharp.business.modules.mall.application.MallSettingApplicationService;
import com.jeesharp.business.modules.mall.dto.MallSettingSaveDTO;
import com.jeesharp.business.modules.mall.entity.MallSetting;
import com.jeesharp.business.modules.mall.vo.MallSettingInfoVO;
import com.jeesharp.business.modules.mall.vo.MallSettingListVO;
import com.jeesharp.business.modules.mall.vo.MallSettingQueryVO;
import com.jeesharp.framework.pojo.AjaxResult;
import com.jeesharp.framework.pojo.Page;
import com.jeesharp.framework.pojo.query.PageRespVO;
import com.jeesharp.framework.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 商城设置管理 Controller
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/manager/business/mall/setting/v1")
@Tag(name = "商城设置管理")
@AllArgsConstructor
public class MallSettingController extends BaseController {

    private final MallSettingApplicationService mallSettingApplicationService;

    @Operation(summary = "获取详情")
    @GetMapping(value = "info")
    public AjaxResult<MallSettingInfoVO> info(@RequestParam("id") String id) {
        MallSettingInfoVO vo = mallSettingApplicationService.get(id);
        return AjaxResult.okData(vo);
    }

    @Operation(summary = "获取列表（分页）")
    @PostMapping(value = "list")
    public AjaxResult<PageRespVO<MallSettingListVO>> list(@RequestBody MallSettingQueryVO query) {
        PageRespVO<MallSettingListVO> page = mallSettingApplicationService.findPage(new Page<>(query), query);
        return AjaxResult.okData(page);
    }

    @Operation(summary = "保存")
    @PostMapping(value = "save")
    public AjaxResult<Void> save(@RequestBody @Valid MallSettingSaveDTO dto) {
        mallSettingApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "删除")
    @GetMapping(value = "delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        mallSettingApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }

    @Operation(summary = "获取配置值")
    @GetMapping(value = "getValue")
    public AjaxResult<String> getValue(@RequestParam("configKey") String configKey) {
        String value = mallSettingApplicationService.getConfigValue(configKey);
        return AjaxResult.okData(value);
    }

    @Operation(summary = "设置配置值")
    @PostMapping(value = "setValue")
    public AjaxResult<Void> setValue(@RequestParam("configKey") String configKey,
                              @RequestParam("configValue") String configValue) {
        mallSettingApplicationService.setConfigValue(configKey, configValue);
        return AjaxResult.ok("设置成功");
    }

    @Operation(summary = "获取分组配置")
    @GetMapping(value = "getByGroup")
    public AjaxResult<List<MallSetting>> getByGroup(@RequestParam("configGroup") String configGroup) {
        List<MallSetting> settings = mallSettingApplicationService.getSettingsByGroup(configGroup);
        return AjaxResult.okData(settings);
    }

    @Operation(summary = "初始化系统配置")
    @PostMapping(value = "initSystemSettings")
    public AjaxResult<Void> initSystemSettings() {
        mallSettingApplicationService.initSystemSettings();
        return AjaxResult.ok("系统配置初始化成功");
    }
}
