package com.multictrl.modules.business.controller;

import cn.hutool.json.JSONObject;
import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.DataFilter;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.constant.Constant;
import com.multictrl.common.page.PageData;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.AssertUtils;
import com.multictrl.modules.business.dto.FlightTaskDTO;
import com.multictrl.modules.business.dto.MediaFileDTO;
import com.multictrl.modules.business.service.MediaFileService;
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.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 媒体图库
 *
 * @author Sdy
 * @since 1.0.0 2026/5/27
 */
@RestController
@RequestMapping("business/media")
@Tag(name = "媒体图库")
@ApiOrder(8)
@RequiredArgsConstructor
public class MediaFileController {
    private final MediaFileService mediaFileService;

    @GetMapping("/page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "dockSn", description = "机库sn"),
            @Parameter(name = "routeId", description = "航线id"),
            @Parameter(name = "startTime", description = "开始时间"),
            @Parameter(name = "endTime", description = "结束时间")
    })
    @DataFilter(tableAlias = "t")
    @RequiresPermissions("bus:media:page")
    public Result<PageData<JSONObject>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<JSONObject> page = mediaFileService.pageList(params);

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

    @GetMapping("/getAlbum")
    @Operation(summary = "获取相簿")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "dockSn", description = "机库sn")
    })
    @DataFilter(tableAlias = "t")
    @RequiresPermissions("bus:media:album")
    public Result<PageData<MediaFileDTO>> getAlbum(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<MediaFileDTO> page = mediaFileService.getAlbum(params);

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

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除媒体文件")
    @RequiresPermissions("bus:media:delete")
    public Result<Object> delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        mediaFileService.deleteMediaFile(ids);

        return new Result<>();
    }

    @Operation(summary = "下载媒体文件")
    @LogOperation("下载媒体文件")
    @GetMapping("/downloadMedia/{id}")
    @RequiresPermissions("bus:media:download")
    public ResponseEntity<InputStreamResource> downloadMedia(@PathVariable Long id) {

        return mediaFileService.downloadMedia(id);
    }

    @Operation(summary = "条件下载媒体文件")
    @LogOperation("条件下载媒体文件")
    @GetMapping("/conditionDownloadMedia")
    @RequiresPermissions("bus:media:download")
    @Parameters({
            @Parameter(name = "dockSn", description = "机库sn"),
            @Parameter(name = "routeId", description = "航线标识"),
            @Parameter(name = "startTime", description = "开始时间"),
            @Parameter(name = "endTime", description = "结束时间")
    })
    @DataFilter(tableAlias = "t")
    public ResponseEntity<InputStreamResource> conditionDownloadMedia(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {

        return mediaFileService.downloadMediaByZip(params);
    }

    @Operation(summary = "一键获取下载媒体地址")
    @LogOperation("一键获取下载媒体地址")
    @GetMapping("/oneClickGetDownloadPath")
    @RequiresPermissions("bus:media:download")
    @Parameters({
            @Parameter(name = "dockSn", description = "机库sn"),
            @Parameter(name = "routeId", description = "航线标识"),
            @Parameter(name = "startTime", description = "开始时间"),
            @Parameter(name = "endTime", description = "结束时间")
    })
    @DataFilter(tableAlias = "t")
    public Result<Map<String, List<String>>> oneClickGetDownloadPath(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        Map<String, List<String>> map = mediaFileService.oneClickGetDownloadPath(params);

        return new Result<Map<String, List<String>>>().ok(map);
    }

    @DeleteMapping("/conditionDeleteMedia")
    @Operation(summary = "条件删除媒体文件 -- 参数和条件下载一样，形式为json")
    @LogOperation("条件删除媒体文件")
    @RequiresPermissions("bus:media:delete")
    public Result<Object> conditionDeleteMedia(@RequestBody Map<String, Object> params) {
        mediaFileService.conditionDeleteMedia(params);

        return new Result<>();
    }
}
