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

import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.oss.api.dto.ServerInfo;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.file.api.constant.UploadChannelType;
import cn.reghao.tnb.file.app.rpc.StoreServiceWrapperRouter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
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 StoreServiceWrapperRouter storeServiceWrapperRouter;

    public FileOssController(StoreServiceWrapperRouter storeServiceWrapperRouter) {
        this.storeServiceWrapperRouter = storeServiceWrapperRouter;
    }

    @AuthUser
    @Operation(summary = "获取图片上传 token", description = "N")
    @PostMapping(value = "/serverinfo/image", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getImageChannelInfo() throws Exception {
        String channelName = UploadChannelType.imageChannel.name();
        ServerInfo serverInfo = storeServiceWrapperRouter.getUploadStore(channelName);
        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 {
        String channelName = UploadChannelType.photoChannel.name();
        ServerInfo serverInfo = storeServiceWrapperRouter.getUploadStore(channelName);
        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 {
        String channelName = UploadChannelType.videoChannel.name();
        ServerInfo serverInfo = storeServiceWrapperRouter.getUploadStore(channelName);
        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 {
        String channelName = UploadChannelType.fileChannel.name();
        ServerInfo serverInfo = storeServiceWrapperRouter.getUploadStore(channelName);
        return serverInfo != null ? WebResult.success(serverInfo) : WebResult.failWithMsg("get ServerInfo failed");
    }
}
