package com.haha.admin.controller;

import com.haha.admin.annotation.RequirePermission;
import com.haha.common.vo.Result;
import com.haha.service.OssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * OSS 上传控制器
 */
@Slf4j
@RestController
@RequestMapping("/oss")
@RequiredArgsConstructor
public class OssUploadController {

    private final OssService ossService;

    /**
     * 获取前端直传 OSS 的临时凭证
     *
     * @param dir 上传目录前缀，默认为 "shop-archive"
     * @return 包含 STS 临时凭证的 Map
     */
    @RequirePermission("shop:archive:create")
    @GetMapping("/upload-credential")
    public Result<Map<String, String>> getUploadCredential(
            @RequestParam(value = "dir", defaultValue = "shop-archive") String dir) {
        Map<String, String> credential = ossService.getUploadCredential(dir);
        return Result.success("获取凭证成功", credential);
    }

    /**
     * 删除 OSS 文件
     *
     * @param objectKey OSS 对象键
     * @return 删除结果
     */
    @RequirePermission("shop:archive:delete")
    @DeleteMapping("/file")
    public Result<Void> deleteFile(@RequestParam String objectKey) {
        ossService.deleteByObjectKey(objectKey);
        return Result.success("删除成功", null);
    }
}
