package de.accso.flexinale.backoffice.api_in.rest;

import de.accso.flexinale.backoffice.application.services.FilmUploadService;
import de.accso.flexinale.backoffice.domain.model.Film;
import de.accso.flexinale.backoffice.application.services.FilmService;
import de.accso.flexinale.common.shared_kernel.Identifiable;
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
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) {
        Identifiable.Id filmId = Identifiable.Id.of(id);
        Film film = filmService.film(filmId);
        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()));
    }
}
