package de.jottyfan.camporganizer.module.business.bookings;

import java.time.LocalDate;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import de.jottyfan.camporganizer.module.business.bookings.model.AddPaymentBean;
import de.jottyfan.camporganizer.module.business.bookings.model.BookerBean;
import de.jottyfan.camporganizer.module.camplist.CommonController;
import jakarta.annotation.security.RolesAllowed;

/**
*
* @author jotty
*
*/
@Controller
public class BookingsController extends CommonController {
	private static final Logger LOGGER = LogManager.getLogger(BookingsController.class);

	@Autowired
	private BookingsService bookingsService;

	@GetMapping("/business/bookings/{year}")
	@RolesAllowed({"business_booking"})
	public String getBookings(Model model, @Nullable @PathVariable("year") Integer year, @RequestParam(name = "search", defaultValue = "") String search) {
		model.addAttribute("bookers", bookingsService.getBookers(getCurrentUser(), year));
		model.addAttribute("addBean", new AddPaymentBean());
		model.addAttribute("year", year == null ? LocalDate.now().getYear() : year);
		model.addAttribute("years", bookingsService.getAllCampYears());
		model.addAttribute("search", search);
		return "business/bookings";
	}

	@GetMapping("/business/bookings_by_id/{id}")
	@RolesAllowed({"business_booking"})
	public String getBooking(Model model, @PathVariable("id") Integer id) {
		BookerBean bean = bookingsService.getBooker(id, getCurrentUser());
		model.addAttribute("booker", bean);
		model.addAttribute("addBean", new AddPaymentBean());
		return bean == null ? getBookings(model, LocalDate.now().getYear(), null) : "business/booker";
	}

	@PostMapping("/business/bookings/payment/{id}")
	@RolesAllowed({"business_booking"})
	public String addPayment(Model model, @ModelAttribute("bean") AddPaymentBean bean, @PathVariable(name = "id") Integer id) {
		Double payment = bean.getPayment();
		bookingsService.addPayment(id, payment);
		return getBooking(model, id);
	}

	@PostMapping("/business/bookings/listpayment/{id}/{year}")
	@RolesAllowed({"business_booking"})
	public String addListPayment(Model model, @ModelAttribute("bean") AddPaymentBean bean, @PathVariable(name = "id") Integer id, @PathVariable(name = "year") Integer year, @RequestParam(name = "search", defaultValue = "") String search) {
		Double payment = bean.getPayment();
		bookingsService.addPayment(id, payment);
		LOGGER.debug("search is {}", search);
		model.addAttribute("search", search);
		return "redirect:/business/bookings/" + year + "?search=" + search;
	}
}
