package cn.reghao.bnt.web.devops.aliyun.controller;

import cn.reghao.bnt.web.devops.aliyun.model.dto.GetSignedUrl;
import cn.reghao.bnt.web.devops.aliyun.model.po.AliyunAccount;
import cn.reghao.bnt.web.devops.aliyun.model.vo.StsToken;
import cn.reghao.bnt.web.devops.aliyun.service.AliyunAccountService;
import cn.reghao.bnt.web.devops.aliyun.service.AliyunService;
import cn.reghao.jutil.web.WebResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2025-03-28 09:30:20
 */
@Tag(name = "阿里云管理页面")
@RestController
@RequestMapping("/api/devops/machine/aliyun")
public class AliyunController {
    private final AliyunAccountService aliyunAccountService;
    private final AliyunService aliyunService;

    public AliyunController(AliyunAccountService aliyunAccountService, AliyunService aliyunService) {
        this.aliyunAccountService = aliyunAccountService;
        this.aliyunService = aliyunService;
    }

    @Operation(summary = "阿里云 key 页面", description = "N")
    @GetMapping("/key")
    public String aliyunKeyPage() {
        Page<AliyunAccount> page = aliyunAccountService.getAliyunAccounts();
        List<AliyunAccount> list = page.getContent();
        return WebResult.success(list);
    }

    @Operation(summary = "获取阿里云 sts token", description = "N")
    @PostMapping(value = "/sts_token", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getUploadStore() {
        StsToken stsToken = aliyunService.getStsToken();
        return WebResult.success(stsToken);
    }

    @Operation(summary = "获取阿里云 oss 签名 url", description = "N")
    @PostMapping(value = "/signed_url", produces = MediaType.APPLICATION_JSON_VALUE)
    public String getSignedUrl(@RequestBody @Validated GetSignedUrl getSignedUrl) {
        String signedUrl = aliyunService.getSignedUrl(getSignedUrl.getObjectName());
        return WebResult.success(signedUrl);
    }
}
