package cn.reghao.tnb.file.app.controller;

import cn.reghao.oss.api.dto.rest.UploadFileRet;
import cn.reghao.oss.api.dto.rest.UploadPrepare;
import cn.reghao.oss.api.dto.rest.UploadPrepareRet;
import cn.reghao.oss.api.dto.rest.UploadSample;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.oss.api.dto.ServerInfo;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.file.app.model.constant.UploadChannelType;
import cn.reghao.tnb.file.app.service.OssUploadService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author reghao
 * @date 2024-04-19 20:47:14
 */
@Tag(name = "OSS 接口")
@RestController
@RequestMapping("/api/file/oss")
public class FileOssController {
    private final OssUploadService ossUploadService;

    public FileOssController(OssUploadService ossUploadService) {
        this.ossUploadService = ossUploadService;
    }

    @AuthUser
    @Operation(summary = "获取图片上传 token", description = "N")
    @PostMapping(value = "/serverinfo/image", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getImageChannelInfo() throws Exception {
        int channelCode = UploadChannelType.imageChannel.getChannelCode();
        ServerInfo serverInfo = ossUploadService.getUploadStore(channelCode);
        return serverInfo != null ? WebResult.success(serverInfo) : WebResult.failWithMsg("get ServerInfo failed");
    }

    @AuthUser
    @Operation(summary = "获取照片上传 token", description = "N")
    @PostMapping(value = "/serverinfo/photo", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getPhotoChannelInfo() throws Exception {
        int channelCode = UploadChannelType.photoChannel.getChannelCode();
        ServerInfo serverInfo = ossUploadService.getUploadStore(channelCode);
        return serverInfo != null ? WebResult.success(serverInfo) : WebResult.failWithMsg("get ServerInfo failed");
    }

    @AuthUser
    @Operation(summary = "获取视频上传 token", description = "N")
    @PostMapping(value = "/serverinfo/video", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getVideoChannelInfo() throws Exception {
        int channelCode = UploadChannelType.videoChannel.getChannelCode();
        ServerInfo serverInfo = ossUploadService.getUploadStore(channelCode);
        return serverInfo != null ? WebResult.success(serverInfo) : WebResult.failWithMsg("get ServerInfo failed");
    }

    @AuthUser
    @Operation(summary = "获取文件上传 token", description = "N")
    @PostMapping(value = "/serverinfo/file", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getFileChannelInfo() throws Exception {
        int channelCode = UploadChannelType.fileChannel.getChannelCode();
        ServerInfo serverInfo = ossUploadService.getUploadStore(channelCode);
        return serverInfo != null ? WebResult.success(serverInfo) : WebResult.failWithMsg("get ServerInfo failed");
    }

    @AuthUser
    @Operation(summary = "文件上传前的预检", description = "N")
    @PostMapping(value = "/prepare_upload", produces = MediaType.APPLICATION_JSON_VALUE)
    public String prepareUpload(@RequestBody @Validated UploadPrepare uploadPrepare) throws Exception {
        UploadPrepareRet uploadPrepareRet = ossUploadService.prepareUpload(uploadPrepare);
        return WebResult.success(uploadPrepareRet);
    }

    @AuthUser
    @Operation(summary = "文件快传的二次检查", description = "N")
    @PostMapping(value = "/check_sample", produces = MediaType.APPLICATION_JSON_VALUE)
    public String checkSample(@RequestBody @Validated UploadSample uploadSample) throws Exception {
        UploadFileRet uploadFileRet = ossUploadService.checkSample(uploadSample);
        return WebResult.success(uploadFileRet);
    }
}
