package com.multictrl.modules.business.q20.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.page.PageData;
import com.multictrl.common.utils.Result;
import com.multictrl.modules.business.dto.RouteDTO;
import com.multictrl.modules.business.q20.dto.*;
import com.multictrl.modules.business.q20.service.Q20RouteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * Q20航线任务接口
 * topic: thing/device/{device_sn}/services (下行)
 * 回复: thing/device/{device_sn}/services_reply (上行)
 *
 * @author 938693313@qq.com
 * @since 1.0.0 2026/5/20
 */
@RestController
@RequestMapping("business/q20/route")
@Tag(name = "Q20航线任务")
@ApiOrder(13)
@RequiredArgsConstructor
public class Q20RouteController {

    private final Q20RouteService q20RouteService;

    // ==================== 航线库分页查询 ====================

    @GetMapping("/page")
    @Operation(summary = "分页查询本地Q20航线（含航点），支持按航线名称/航线ID查询")
    @RequiresPermissions("bus:q20:route")
    public Result<PageData<RouteDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        return new Result<PageData<RouteDTO>>().ok(q20RouteService.pageQ20Routes(params));
    }

    // ==================== 下一个航线ID ====================

    @GetMapping("/nextWayline")
    @Operation(summary = "获取下一个航线ID（根据库中最新航线ID自动递增）")
    @RequiresPermissions("bus:q20:route")
    public Result<String> nextWayline(@RequestParam(defaultValue = "route") String type) {
        return new Result<String>().ok(q20RouteService.nextWayline(type));
    }

    // ==================== 上传航线 ====================

    @PostMapping("/upload/{deviceSn}")
    @LogOperation("上传航线")
    @Operation(summary = "上传航线（route_upload）")
    @RequiresPermissions("bus:q20:route")
    public Result<Object> routeUpload(@PathVariable String deviceSn,
                                      @RequestBody Q20RouteUploadDTO dto) {
        return new Result<>().ok(q20RouteService.routeUpload(deviceSn, dto));
    }

    // ==================== 执行航线 ====================

    @PostMapping("/execute/{deviceSn}")
    @LogOperation("执行航线")
    @Operation(summary = "执行航线（route_execute）")
    @RequiresPermissions("bus:q20:route")
    public Result<Object> routeExecute(@PathVariable String deviceSn,
                                       @RequestBody Q20RouteExecuteDTO dto) {
        return new Result<>().ok(q20RouteService.routeExecute(deviceSn, dto));
    }

    // ==================== 一键飞行 ====================

    @PostMapping("/auto/{deviceSn}")
    @LogOperation("一键飞行（上传+执行）")
    @Operation(summary = "一键飞行，上传并执行航线（route_auto）")
    @RequiresPermissions("bus:q20:route")
    public Result<Object> routeAuto(@PathVariable String deviceSn,
                                    @RequestBody Q20RouteAutoDTO dto) {
        return new Result<>().ok(q20RouteService.routeAuto(deviceSn, dto));
    }

    // ==================== 取消悬停 ====================

    @PostMapping("/breakHover/{deviceSn}")
    @LogOperation("取消悬停")
    @Operation(summary = "取消悬停（break_loop_hover）")
    @RequiresPermissions("bus:q20:route")
    public Result<Object> breakLoopHover(@PathVariable String deviceSn) {
        return new Result<>().ok(q20RouteService.breakLoopHover(deviceSn));
    }

    // ==================== 获取航线信息（暂时停用，先注释） ====================

    @GetMapping("/info/{deviceSn}")
    @Operation(summary = "获取航线信息（route_info）")
    @RequiresPermissions("bus:q20:route")
    public Result<Object> routeInfo(@PathVariable String deviceSn,
                                    @RequestParam int mode,
                                    @RequestParam(required = false) String value) {
        return new Result<>().ok(q20RouteService.routeInfo(deviceSn, mode, value));
    }

    // ==================== 获取执行进度（暂时停用，先注释） ====================

//    @GetMapping("/progress/{deviceSn}")
//    @Operation(summary = "获取航线执行进度（route_progress）")
//    @RequiresPermissions("bus:q20:route")
//    public Result<Object> routeProgress(@PathVariable String deviceSn) {
//        return new Result<>().ok(q20RouteService.routeProgress(deviceSn));
//    }

    // ==================== 暂停航线 ====================

    @PostMapping("/pause/{deviceSn}")
    @LogOperation("暂停航线")
    @Operation(summary = "暂停航线（route_pause）")
    @RequiresPermissions("bus:q20:route")
    public Result<Object> routePause(@PathVariable String deviceSn) {
        return new Result<>().ok(q20RouteService.routePause(deviceSn));
    }

    // ==================== 继续航线 ====================

    @PostMapping("/resume/{deviceSn}")
    @LogOperation("继续航线")
    @Operation(summary = "继续航线（route_resume）")
    @RequiresPermissions("bus:q20:route")
    public Result<Object> routeResume(@PathVariable String deviceSn) {
        return new Result<>().ok(q20RouteService.routeResume(deviceSn));
    }

    // ==================== 退出航线（暂时停用，先注释） ====================

//    @PostMapping("/finish/{deviceSn}")
//    @LogOperation("退出航线")
//    @Operation(summary = "退出航线（route_finish）")
//    @RequiresPermissions("bus:q20:route")
//    public Result<Object> routeFinish(@PathVariable String deviceSn) {
//        return new Result<>().ok(q20RouteService.routeFinish(deviceSn));
//    }

    // ==================== 获取围栏信息（暂时停用，先注释） ====================

//    @GetMapping("/geofenceInfo/{deviceSn}")
//    @Operation(summary = "获取围栏信息（geofence_info）")
//    @RequiresPermissions("bus:q20:route")
//    public Result<Object> geofenceInfo(@PathVariable String deviceSn) {
//        return new Result<>().ok(q20RouteService.geofenceInfo(deviceSn));
//    }

    // ==================== 上传围栏（暂时停用，先注释） ====================

//    @PostMapping("/geofenceUpload/{deviceSn}")
//    @LogOperation("上传围栏")
//    @Operation(summary = "上传围栏（geofence_upload）")
//    @RequiresPermissions("bus:q20:route")
//    public Result<Object> geofenceUpload(@PathVariable String deviceSn,
//                                         @RequestBody Q20GeofenceUploadDTO dto) {
//        return new Result<>().ok(q20RouteService.geofenceUpload(deviceSn, dto));
//    }
}
