package com.corewing.app.modules.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.corewing.app.common.Result;
import com.corewing.app.common.annotation.CommonLog;
import com.corewing.app.entity.SiriusFirmware;
import com.corewing.app.service.SiriusFirmwareService;
import com.corewing.app.util.I18nUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Sirius固件信息 Controller
 */
@Api(tags = "Sirius固件信息接口")
@RestController
@RequestMapping("/sirius_firmware")
public class AppSiriusFirmwareController {

    private final SiriusFirmwareService siriusFirmwareService;

    public AppSiriusFirmwareController(SiriusFirmwareService siriusFirmwareService) {
        this.siriusFirmwareService = siriusFirmwareService;
    }

    /**
     * 根据固件类型和协议类型查询最新版本
     */
    @CommonLog("查询Sirius最新固件版本")
    @ApiOperation("根据固件类型和协议类型查询最新版本")
    @GetMapping("/latest")
    public Result<SiriusFirmware> getLatest(
            @RequestParam Integer firmwareType,
            @RequestParam Integer protocolType) {
        SiriusFirmware firmware = siriusFirmwareService.getLatestByTypeAndProtocol(firmwareType, protocolType);
        if (firmware != null) {
            return Result.success(firmware);
        }
        return Result.error(I18nUtil.getMessage("firmware.not.found"));
    }

    /**
     * 根据固件类型查询版本列表
     */
    @CommonLog("根据类型查询Sirius固件版本列表")
    @ApiOperation("根据固件类型查询版本列表")
    @GetMapping("/type/{firmwareType}")
    public Result<List<SiriusFirmware>> listByType(@PathVariable Integer firmwareType) {
        QueryWrapper<SiriusFirmware> wrapper = new QueryWrapper<>();
        wrapper.eq("firmware_type", firmwareType);
        wrapper.orderByDesc("create_time");
        return Result.success(siriusFirmwareService.list(wrapper));
    }

    /**
     * 根据固件类型和协议类型查询版本列表
     */
    @CommonLog("根据类型和协议查询Sirius固件版本列表")
    @ApiOperation("根据固件类型和协议类型查询版本列表")
    @GetMapping("/type/{firmwareType}/protocol/{protocolType}")
    public Result<List<SiriusFirmware>> listByTypeAndProtocol(
            @PathVariable Integer firmwareType,
            @PathVariable Integer protocolType) {
        QueryWrapper<SiriusFirmware> wrapper = new QueryWrapper<>();
        wrapper.eq("firmware_type", firmwareType);
        wrapper.eq("protocol_type", protocolType);
        wrapper.orderByDesc("create_time");
        return Result.success(siriusFirmwareService.list(wrapper));
    }

    /**
     * 查询所有固件
     */
    @CommonLog("查询所有Sirius固件")
    @ApiOperation("查询所有固件")
    @GetMapping("/list")
    public Result<List<SiriusFirmware>> list() {
        return Result.success(siriusFirmwareService.list());
    }

    /**
     * 根据ID查询固件
     */
    @CommonLog("根据ID查询Sirius固件")
    @ApiOperation("根据ID查询固件")
    @GetMapping("/{id}")
    public Result<SiriusFirmware> getById(@PathVariable Integer id) {
        SiriusFirmware firmware = siriusFirmwareService.getById(id);
        if (firmware != null) {
            return Result.success(firmware);
        }
        return Result.error(I18nUtil.getMessage("firmware.not.found"));
    }
}
