package com.multictrl.modules.business.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.ValidatorUtils;
import com.multictrl.modules.business.dto.zhimou.DownloadReport;
import com.multictrl.modules.business.dto.zhimou.Osd;
import com.multictrl.modules.business.dto.zhimou.ZhiMouAiStart;
import com.multictrl.modules.business.service.ZhiMouService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 智眸AI接口封装
 *
 * @author Sdy
 * @since 1.0.0 2026/6/9
 */
@RestController
@RequestMapping("business/zhiMou")
@Tag(name = "智眸AI接口")
@ApiOrder(23)
@RequiredArgsConstructor
public class ZhiMouController {
    private final ZhiMouService zhiMouService;

    @Operation(summary = "获取应用列表")
    @GetMapping("/getApp")
    @RequiresPermissions("bus:zhimou:get")
    public Result<Object> getApp() {
        JSONObject data = zhiMouService.getApp();
        return zhiMouService.resultHandle(data);
    }

    @Operation(summary = "开始AI检测任务")
    @LogOperation("开始AI检测任务")
    @PostMapping("/startByWeb")
    @RequiresPermissions("bus:zhimou:start")
    public Result<Object> startByWeb(@RequestBody ZhiMouAiStart zhiMouAiStart) {
        ValidatorUtils.validateEntity(zhiMouAiStart);
        JSONObject data = zhiMouService.startByWeb(zhiMouAiStart);
        return zhiMouService.resultHandle(data);
    }

    @Operation(summary = "停止AI检测任务")
    @LogOperation("停止AI检测任务")
    @PostMapping("/stopByWeb")
    @RequiresPermissions("bus:zhimou:stop")
    public Result<Object> stopByWeb(@RequestParam String dockSn) {
        JSONObject data = zhiMouService.stopByWeb(dockSn);
        return zhiMouService.resultHandle(data);
    }

    @Operation(summary = "获取AI检测任务")
    @GetMapping("/getRunningTaskByWeb")
    public Result<Object> getRunningTaskByWeb(@RequestParam String dockSn) {
        JSONObject data = zhiMouService.getRunningTaskByWeb(dockSn);
        return new Result<>().ok(data);
    }

    @Operation(summary = "配置AI检测任务")
    @LogOperation("配置AI检测任务")
    @PostMapping("/configurationByWeb")
    @RequiresPermissions("bus:zhimou:config")
    public Result<Object> configurationByWeb(@RequestBody Osd osd) {
        JSONObject data = zhiMouService.configurationByWeb(osd);
        return zhiMouService.resultHandle(data);
    }

    @Operation(summary = "获取实时检测结果")
    @GetMapping("/currentTaskMessageRecord")
    public Result<Object> currentTaskMessageRecord(@RequestParam String dockSn) {
        JSONObject data = zhiMouService.currentTaskMessageRecord(dockSn);
        return zhiMouService.resultHandle(data);
    }

    @Operation(summary = "获取飞行任务检测结果")
    @GetMapping("/getTaskMessageRecord")
    @RequiresPermissions("bus:zhiMou:image")
    public Result<Object> getTaskMessageRecordList(@RequestParam String taskId) {
        JSONArray data = zhiMouService.getTaskMessageRecordList(taskId);
        return new Result<>().ok(data);
    }

    @Operation(summary = "下载AI检测报告")
    @PostMapping("/downloadReport")
    @RequiresPermissions("bus:zhiMou:downloadReport")
    public ResponseEntity<byte[]> getReportByBusinessId(@RequestBody DownloadReport downloadReport) {
        ValidatorUtils.validateEntity(downloadReport);
        byte[] reportBytes = zhiMouService.downloadReport(downloadReport);

        String fileName = "AI报告_" + downloadReport.getTaskId() + ".zip";
        String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + encodedFileName)
                .body(reportBytes);
    }

    @Operation(summary = "是否有AI服务")
    @GetMapping("/hasAiServer")
    @RequiresPermissions("bus:zhiMou:hasAiServer")
    public Result<Object> hasAiServer() {
        boolean result = zhiMouService.hasAiService();

        return new Result<>().ok(result);
    }
}
