package com.corewing.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.corewing.app.common.Result;
import com.corewing.app.entity.Firmware;
import com.corewing.app.service.FirmwareService;
import com.corewing.app.util.I18nUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 固件 Controller
 */
@RestController
@RequestMapping("/firmware")
public class FirmwareController {

    private final FirmwareService firmwareService;

    public FirmwareController(FirmwareService firmwareService) {
        this.firmwareService = firmwareService;
    }

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

    /**
     * 分页查询固件列表
     *
     * @param current      当前页
     * @param size         每页大小
     * @param firmwareName 固件名称（可选）
     * @param firmwareType 固件类型（可选）
     */
    @GetMapping("/page")
    public Result<IPage<Firmware>> page(
            @RequestParam(defaultValue = "1") Long current,
            @RequestParam(defaultValue = "10") Long size,
            @RequestParam(required = false) String firmwareName,
            @RequestParam(required = false) String firmwareType) {

        Page<Firmware> page = new Page<>(current, size);
        QueryWrapper<Firmware> wrapper = new QueryWrapper<>();

        // 根据固件名称模糊查询
        if (StringUtils.hasText(firmwareName)) {
            wrapper.like("firmware_name", firmwareName);
        }

        // 根据固件类型查询
        if (StringUtils.hasText(firmwareType)) {
            wrapper.eq("firmware_type", firmwareType);
        }

        // 按创建时间倒序排列
        wrapper.orderByDesc("create_time");

        IPage<Firmware> result = firmwareService.page(page, wrapper);
        return Result.success(result);
    }

    /**
     * 查询所有固件
     */
    @GetMapping("/list")
    public Result<java.util.List<Firmware>> list() {
        java.util.List<Firmware> list = firmwareService.list();
        return Result.success(list);
    }

    /**
     * 根据类型查询固件版本
     *
     * @param firmwareType 固件类型
     */
    @GetMapping("/type/{firmwareType}")
    public Result<java.util.List<Firmware>> listByType(@PathVariable Integer firmwareType) {
        if (firmwareType == null) {
            return Result.error(I18nUtil.getMessage("firmware.type.required"));
        }

        QueryWrapper<Firmware> wrapper = new QueryWrapper<>();
        wrapper.eq("firmware_type", firmwareType);
        // 按版本号或创建时间倒序排列，最新版本在前
        wrapper.orderByDesc("create_time");

        java.util.List<Firmware> list = firmwareService.list(wrapper);
        return Result.success(list);
    }
}
