package com.mvrent.carrental.controller;

import com.mvrent.carrental.service.FileStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/** Generic single-file upload — stores the file and returns its public URL. */
@RestController
@RequestMapping("/admin/api/upload")
@RequiredArgsConstructor
public class AdminUploadApiController {

    private final FileStorageService fileStorage;

    @PostMapping
    public ResponseEntity<?> upload(@RequestParam MultipartFile file) {
        String url = fileStorage.store(file);
        if (url == null) {
            return ResponseEntity.badRequest().body(Map.of("ok", false, "message", "No file uploaded"));
        }
        return ResponseEntity.ok(Map.of("ok", true, "url", url));
    }
}
