package com.snack.server.module.file.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.snack.server.common.Result;
import com.snack.server.module.file.service.FileService;
import com.snack.server.module.file.vo.FileUploadVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传（管理端 + 用户端通用）
 */
@Tag(name = "文件上传")
@RestController
@RequestMapping("/api/file")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    /**
     * 上传文件
     * multipart/form-data，字段名 file
     */
    @Operation(summary = "上传文件（支持图片/通用文件）")
    @SaCheckLogin
    @PostMapping("/upload")
    public Result<FileUploadVO> upload(
            @Parameter(description = "文件") @RequestParam("file") MultipartFile file,
            @Parameter(description = "业务类型：avatar / product / banner / notice / chat / common")
            @RequestParam(defaultValue = "common") String bizType) {
        return Result.ok(fileService.upload(file, bizType));
    }

    @Operation(summary = "删除文件")
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public Result<Void> delete(@Parameter(description = "文件 ID") @PathVariable Long id) {
        fileService.deleteFile(id);
        return Result.ok();
    }
}
