package ru.micord.ervu_dashboard.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import ru.micord.ervu_dashboard.service.AppealsFileUploadService;

/**
 * @author Adel Kalimullin
 */
@RestController
public class AppealsFileUploadController {
  private static final Logger LOGGER = LoggerFactory.getLogger(AppealsFileUploadController.class);
  private final AppealsFileUploadService fileUploadService;

  public AppealsFileUploadController(AppealsFileUploadService fileUploadService) {
    this.fileUploadService = fileUploadService;
  }

  @PostMapping("/appeals/document")
  public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) {
    if (file.isEmpty()) {
      return ResponseEntity.badRequest().body("No file provided.");
    }
    try {
      fileUploadService.save(file);
      return ResponseEntity.ok().body("File successfully uploaded.");
    }
    catch (Exception e) {
      LOGGER.error(e.getMessage(), e);
      return ResponseEntity.internalServerError().body("An error occurred while uploading file.");
    }
  }
}
