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

import cn.hutool.core.util.StrUtil;

import com.jeesharp.business.modules.setting.application.ClientAppVersionManageApplicationService;
import com.jeesharp.business.modules.setting.dto.ClientAppVersionSaveDTO;
import com.jeesharp.business.modules.setting.entity.CfgClientAppVersion;
import com.jeesharp.business.modules.setting.vo.ClientAppVersionInfoVO;
import com.jeesharp.business.modules.setting.vo.ClientAppVersionListVO;
import com.jeesharp.business.modules.setting.vo.ClientAppVersionQueryVO;
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 jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * C 端客户端版本发布管理（后台管理端），维护小程序/App 检查更新与更新日志数据源。
 *
 * @author jeesharp
 * @since 2026-05-13
 */
@RestController
@RequestMapping(value = "${jeesharp.web.adminPath}/setting/clientAppVersion/v1")
@Tag(name = "站点设置-客户端版本发布")
@AllArgsConstructor
public class ClientAppVersionController extends BaseController {

    private final ClientAppVersionManageApplicationService clientAppVersionManageApplicationService;

    @Operation(summary = "获取详情")
    @PreAuthorize("@ss.hasPermission('setting:clientAppVersion:info')")
    @GetMapping("info")
    public AjaxResult<ClientAppVersionInfoVO> info(@RequestParam("id") String id) {
        return AjaxResult.okData(clientAppVersionManageApplicationService.get(id));
    }

    @Operation(summary = "分页列表")
    @PreAuthorize("@ss.hasPermission('setting:clientAppVersion:list')")
    @PostMapping("list")
    public AjaxResult<PageRespVO<ClientAppVersionListVO>> list(@RequestBody ClientAppVersionQueryVO query) {
        return AjaxResult.okData(clientAppVersionManageApplicationService.findPage(new Page<CfgClientAppVersion>(query), query));
    }

    @Operation(summary = "保存")
    @PreAuthorize("@ss.hasPermission('setting:clientAppVersion:save')")
    @PostMapping("save")
    public AjaxResult<Void> save(@RequestBody @Valid ClientAppVersionSaveDTO dto) {
        clientAppVersionManageApplicationService.save(dto);
        return AjaxResult.ok("保存成功");
    }

    @Operation(summary = "删除")
    @PreAuthorize("@ss.hasPermission('setting:clientAppVersion:delete')")
    @GetMapping("delete")
    public AjaxResult<Void> delete(@RequestParam("ids") String ids) {
        clientAppVersionManageApplicationService.delete(ids);
        return AjaxResult.ok("删除成功");
    }
}
