package com.jeesharp.business.common.appversion.controller;

import com.jeesharp.business.common.appversion.service.AppVersionMobileService;
import com.jeesharp.business.common.appversion.vo.AppVersionClientVO;
import com.jeesharp.business.common.appversion.vo.AppVersionListClientVO;
import com.jeesharp.business.common.appversion.vo.CheckUpdateClientVO;
import com.jeesharp.framework.pojo.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 客户端版本更新（免登录）：更新日志列表、检查新版本；数据来自库表 {@code cfg_client_app_version}（站点设置模块维护）。
 *
 * @author jeesharp
 * @since 2026-05-12
 */
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/noAuth/common/appVersion")
@Tag(name = "用户端-版本更新", description = "免登录：版本记录、检查更新")
@AllArgsConstructor
public class AppVersionMobileController {

    private final AppVersionMobileService appVersionMobileService;

    @Operation(summary = "版本更新记录（分页）")
    @GetMapping("list")
    public AjaxResult<AppVersionListClientVO> list(
            @Parameter(description = "页码，从 1 开始")
            @RequestParam(value = "page", defaultValue = "1") int page,
            @Parameter(description = "每页条数，最大 50")
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "端：MINI / H5 / ANDROID / IOS 等，默认 MINI")
            @RequestParam(value = "platform", defaultValue = "MINI") String platform) {
        return AjaxResult.okData(appVersionMobileService.list(page, pageSize, platform));
    }

    @Operation(summary = "最新一条版本（按端）")
    @GetMapping("latest")
    public AjaxResult<AppVersionClientVO> latest(
            @Parameter(description = "端：MINI / H5 / ANDROID / IOS 等，默认 MINI")
            @RequestParam(value = "platform", defaultValue = "MINI") String platform) {
        AppVersionListClientVO first = appVersionMobileService.list(1, 1, platform);
        AppVersionClientVO one = first.getList().isEmpty() ? null : first.getList().get(0);
        return AjaxResult.okData(one);
    }

    @Operation(summary = "检查更新")
    @GetMapping("check")
    public AjaxResult<CheckUpdateClientVO> check(
            @Parameter(description = "当前展示版本号")
            @RequestParam("currentVersion") String currentVersion,
            @Parameter(description = "当前整型版本号，用于比较")
            @RequestParam("currentVersionCode") int currentVersionCode,
            @Parameter(description = "端：MINI / H5 / ANDROID / IOS 等")
            @RequestParam(value = "platform", defaultValue = "MINI") String platform) {
        return AjaxResult.okData(appVersionMobileService.check(currentVersion, currentVersionCode, platform));
    }
}
