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

import java.security.Principal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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 de.jottyfan.camporganizer.module.business.outlay.model.OutlayBean;
import de.jottyfan.camporganizer.module.camplist.CommonController;
import jakarta.annotation.security.RolesAllowed;
import jakarta.validation.Valid;
import jakarta.ws.rs.core.HttpHeaders;

/**
 *
 * @author jotty
 *
 */
@Controller
@RolesAllowed({ "business_outlay" })
public class OutlayController extends CommonController {

	@Autowired
	private OutlayService service;

	@GetMapping("/business/outlay")
	public String getOutlayDashboard(Model model, Principal principal) {
		model.addAttribute("list", service.getListOf(super.getCurrentUser(principal), null));
		model.addAttribute("camps", service.getAllCamps());
		return "/business/outlay/list";
	}

	@GetMapping("/business/outlay/add")
	public String loadAddMask(Model model, Principal principal) {
		model.addAttribute("bean", new OutlayBean().withUser(super.getCurrentUser(principal)));
		model.addAttribute("camps", service.getAllCamps());
		return "/business/outlay/editor";
	}

	@GetMapping("/business/outlay/edit/{id}")
	public String loadAddMask(@PathVariable("id") Integer id, Model model, Principal principal) {
		model.addAttribute("bean", service.getBeanIfAllowedFor(super.getCurrentUser(principal), id));
		model.addAttribute("camps", service.getAllCamps());
		return "/business/outlay/editor";
	}

	@PostMapping("/business/outlay/save")
	public String save(@Valid @ModelAttribute("bean") OutlayBean bean, BindingResult bindingResult, Model model) {
		if (bindingResult.hasErrors()) {
			model.addAttribute("camps", service.getAllCamps());
			return "/business/outlay/editor";
		}
		service.save(bean);
		return "redirect:/business/outlay";
	}

	@GetMapping("/business/outlay/delete/{id}")
	public String delete(@PathVariable("id") Integer id, Principal principal) {
		service.deleteIfAllowedFor(super.getCurrentUser(principal), id);
		return "redirect:/business/outlay";
	}

	@GetMapping("/business/outlay/summary/{campid}")
	public String getSummaryOfCamp(@PathVariable("campid") Integer campId, Model model, Principal principal) {
		model.addAttribute("campid", campId);
		model.addAttribute("list", service.getListOf(null, campId));
		return "/business/outlay/summary";
	}

	@GetMapping("/business/outlay/download/{campid}")
	public ResponseEntity<Resource> generateCsv(@PathVariable("campid") Integer campId) {
		String filename = String.format("rechnung_camp_%d.csv", campId);
		InputStreamResource file = new InputStreamResource(service.getCsv(campId));
		return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)
				.contentType(MediaType.parseMediaType("application/csv")).body(file);
	}
}
