package com.seer.teach.iot.admin.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.seer.teach.iot.admin.req.AddBeatsReq;
import com.seer.teach.iot.admin.req.AddVersionReq;
import com.seer.teach.iot.admin.resp.APKDetailResp;
import com.seer.teach.iot.admin.resp.BeatVersionResp;
import com.seer.teach.iot.admin.resp.DeviceListResp;
import com.seer.teach.iot.admin.service.AdminVersionService;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 版本管理控制器
 * <p>
 * 该控制器用于处理版本相关的管理操作，包括版本的增删改查等功能。
 * * 通过Swagger注解提供API文档支持，使用Sa-Token进行登录权限校验。
 * </p >
 *
 * @author Captain
 * @since 2025-09-11 10:35
 */
@RequestMapping("/tv/version")
@RestController
@Tag(name = "版本管理")
@Validated
@RequiredArgsConstructor
@SaCheckLogin
public class VersionController {

    private final AdminVersionService adminVersionService;

    @PostMapping("/uploadAPK")
    @LogPrint
    @Operation(summary = "上传APK文件")
    public ResultBean<APKDetailResp> uploadVersionApk(@RequestBody @Validated @NotNull(message = "文件不可为空") MultipartFile file) {
        APKDetailResp result = adminVersionService.uploadVersionAPK(file);
        return ResultBean.success(result);
    }

    @PutMapping("/addVersion")
    @LogPrint
    @Operation(summary = "添加版本")
    public ResultBean addVersion(@RequestBody @Validated AddVersionReq params) {
        adminVersionService.addVersion(params);
        return ResultBean.success();
    }


    @PutMapping("/addBeats")
    @LogPrint
    @Operation(summary = "添加内测")
    public ResultBean addBeats(@RequestBody @Validated AddBeatsReq params) {
        adminVersionService.addBeats(params);
        return ResultBean.success();
    }


    @GetMapping("/getBeatVersions")
    @LogPrint
    @Operation(summary = "获取内测版本")
    public ResultBean<List<BeatVersionResp>> getBeantVersions(){
        List<BeatVersionResp> resp = adminVersionService.getBeatVersions();
        return ResultBean.success(resp);
    }


    @DeleteMapping("/deleteBeats")
    @LogPrint
    @Operation(summary = "批量删除内测版本")
    public ResultBean<Void> deleteBeats(@RequestBody @NotEmpty(message = "id列表不可为空") List<Integer> ids) {
        adminVersionService.deleteBeats(ids);
        return ResultBean.success();
    }


    @GetMapping("/getBeats/{page}/{size}")
    @LogPrint
    @Operation(summary = "获取设备内测数据")
    public ResultBean<PageListBean<DeviceListResp>> list(@Parameter(description = "页码") @PathVariable("page") Integer page,
                                                         @Parameter(description = "每页数量") @PathVariable("size") Integer size,
                                                         @Parameter(description = "学生账号") @RequestParam(required = false) String studentAccount) {
        PageListBean<DeviceListResp> result = adminVersionService.versionList(page, size, studentAccount);
        return ResultBean.success(result);
    }


}
