package de.accso.flexinale.rest;

import de.accso.flexinale.application.FilmService;
import de.accso.flexinale.data.services.FilmUploadService;
import de.accso.flexinale.data.model.Film;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Transactional
public class FilmRestController extends AbstractExcelRestController {

    @Autowired
    private FilmService filmService;

    @Autowired
    private FilmUploadService filmUploadService;

    @GetMapping(value = "/rest/filme", produces = "application/json")
    @PreAuthorize("hasRole('ROLE_BESUCHER')")
    public List<Film> filme() {
        return filmService.filme();
    }

    @GetMapping(value = "/rest/film/{id}", produces = "application/json")
    @PreAuthorize("hasRole('ROLE_BESUCHER')")
    public Film film(@PathVariable final String id) {
        Film film = filmService.film(id);
        if (film == null) {
            throw new RestResourceNotFoundException("no Film %s found.".formatted(id));
        }

        return film;
    }

    @PostMapping("/rest/filme")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public ResponseEntity<RestResponseMessage> uploadFilme(@RequestParam("file") final MultipartFile file) {
        RestUploadResult restUploadResult = uploadViaRestCall(filmUploadService, file);
        return ResponseEntity.status(restUploadResult.status())
                .body(new RestResponseMessage(restUploadResult.message()));
    }
}
