package de.accso.flexinale.besucherportal.api_in.web;

import de.accso.flexinale.backoffice.api_contract.event.model.KinoTO;
import de.accso.flexinale.besucherportal.application.services.KinoService;
import de.accso.flexinale.common.shared_kernel.Identifiable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@Controller
@Profile({ "!testdata-backoffice & !testdata-ticketing" })
public class KinoWebController {

    @Autowired
    private KinoService kinoService;

    @GetMapping(value="/kinos")
    @PreAuthorize("hasRole('ROLE_BESUCHER')")
    @SuppressWarnings("SameReturnValue")
    public String kinos(final Model model) {
        List<KinoTO> kinos = kinoService.kinos();
        model.addAttribute("kinos", kinos);
        return "kinos";
    }

    @GetMapping(value="/kino/{id}")
    @PreAuthorize("hasRole('ROLE_BESUCHER')")
    @SuppressWarnings("SameReturnValue")
    public String kino(@PathVariable("id") final String id, final Model model) {
        Identifiable.Id kinoId = Identifiable.Id.of(id);
        KinoTO kino = kinoService.kino(kinoId);
        if (kino == null) {
            throw new ResponseStatusException(HttpStatus.NOT_FOUND, "kino %s not available".formatted(id));
        }
        else {
            model.addAttribute("kino", kino);
            return "kino";
        }
    }
}
