package com.multictrl.modules.business.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.constant.Constant;
import com.multictrl.common.exception.ErrorCode;
import com.multictrl.common.page.PageData;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.AssertUtils;
import com.multictrl.common.validator.ValidatorUtils;
import com.multictrl.modules.business.dto.RemoteLogDTO;
import com.multictrl.modules.business.dto.remote.log.SyncRemoteLog;
import com.multictrl.modules.business.dto.remote.log.RemoteLog;
import com.multictrl.modules.business.service.RemoteLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
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.List;
import java.util.Map;

/**
 * 远程日志
 *
 * @author Sdy
 * @since 1.0.0 2026/5/10
 */
@RestController
@RequestMapping("business/remoteLog")
@Tag(name = "远程日志")
@ApiOrder(10)
@RequiredArgsConstructor
public class RemoteLogController {
    private final RemoteLogService remoteLogService;

    @LogOperation("远程日志列表")
    @PostMapping("/getRemoteLogList/{dockSn}")
    @Operation(summary = "远程日志列表")
    @RequiresPermissions("bus:remoteLog:list")
    public Result<List<RemoteLog>> getRemoteLogList(@PathVariable String dockSn,
                                                    @Parameter(description = "设备类型 0-飞行器 3-机场")
                                                    @RequestParam String module) {
        if (!module.equals("0") && !module.equals("3")) {
            return new Result<List<RemoteLog>>().error(ErrorCode.PARAMS_ERROR);
        }

        return new Result<List<RemoteLog>>().ok(remoteLogService.getRemoteLogList(dockSn, module));
    }

    @LogOperation("同步远程日志")
    @PostMapping("/syncRemoteLog/{dockSn}")
    @Operation(summary = "同步远程日志",
            description = "同步机库或飞机日志到minio,这一步如果成功会立马返回，机库会自动将文件上传到minio，可以通过fileupload_progress监听上传进度")
    @RequiresPermissions("bus:remoteLog:sync")
    public Result<Object> syncRemoteLog(@PathVariable String dockSn,
                                        @RequestBody SyncRemoteLog remoteLog) {
        ValidatorUtils.validateEntity(remoteLog);

        return new Result<>().ok(remoteLogService.syncRemoteLog(dockSn, remoteLog));
    }

    @LogOperation("取消日志同步")
    @PostMapping("/cancelSyncRemoteLog/{dockSn}")
    @Operation(summary = "取消日志同步", description = "当前日志上传启动后，暂时不支持上传任务暂停、暂停后恢复、上传任务拒绝")
    @RequiresPermissions("bus:remoteLog:cancelSync")
    public Result<Object> cancelSyncRemoteLog(@PathVariable String dockSn,
                                              @Parameter(description = "设备类型 0-飞行器 3-机场")
                                              @RequestParam String module) {
        if (!module.equals("0") && !module.equals("3")) {
            return new Result<>().error(ErrorCode.PARAMS_ERROR);
        }

        return new Result<>().ok(remoteLogService.cancelSyncRemoteLog(dockSn, module));
    }

    @GetMapping("page")
    @Operation(summary = "日志列表查询")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "dockSn", description = "机场SN"),
            @Parameter(name = "module", description = "设备类型 0飞机 3机场")
    })
    @RequiresPermissions("bus:remoteLog:page")
    public Result<PageData<RemoteLogDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<RemoteLogDTO> page = remoteLogService.queryPage(params);

        return new Result<PageData<RemoteLogDTO>>().ok(page);
    }

    @DeleteMapping
    @Operation(summary = "删除日志")
    @LogOperation("删除日志")
    @RequiresPermissions("bus:remoteLog:delete")
    public Result<Object> delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        remoteLogService.delete(ids);

        return new Result<>();
    }
}
