package es.uv.saic.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import jakarta.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import es.uv.saic.domain.Usuari;
import es.uv.saic.feign.ManagerClient;

// Controller for the managers administration page 
@Controller
public class ManagersController {
	@Autowired
	private ManagerClient mc;
	
	/*
	 * Load the managers administration page data into the model
	 * - List of centres and titulacions the logged user can manage
	 * - List of all users
	 * - List of assignable roles
	 * - If there are search results in the session, load them too
	 * - If there is a roleExists attribute in the session, load it too
	 * - The view is managers.html
	 */
	@SuppressWarnings("unchecked")
	@GetMapping("/managers")
	public String managersForm(Model model, Authentication auth, HttpSession session) {
		
		HashMap<String, Object> map = mc.managersForm(((Usuari) auth.getPrincipal()).getUsuari());
		model.addAllAttributes(map);
		
	
		if(session.getAttribute("respTitulacions") != null && session.getAttribute("respCentres") != null ) {
			model.addAttribute("restoreCentres", ((List<Integer>)session.getAttribute("respCentres")).toString());
			model.addAttribute("restoreTitulacions", ((List<Integer>)session.getAttribute("respTitulacions")).toString());
			HashMap<String, Object> map2 = mc.managersSearch((List<Integer>)session.getAttribute("respCentres"), (List<Integer>)session.getAttribute("respTitulacions"));
			model.addAllAttributes(map2);
			session.removeAttribute("respTitulacions");
			session.removeAttribute("respCentres");
		}
		
		if(session.getAttribute("roleExists") != null) {
			model.addAttribute("roleExists", true);
			session.removeAttribute("roleExists");
		}
		
		return "managers";
	}
	
	/*
	 * Process the search form submission and load the results into the model
	 * @Param centres List of centre ids to search managers in
	 * @Param titulacions List of titulacion ids to search managers in
	 * @Return The view component list_managers
	 */
	@PostMapping("/managers/search")
	public String managersSearch(Model model, Authentication auth, 
			@RequestParam("center") List<Integer> centres,
			@RequestParam(name="titulation[]", required=false) List<Integer> titulacions,
			HttpSession session) {

		if(titulacions == null) {
			titulacions = new ArrayList<Integer>();
		}
		HashMap<String, Object> map = mc.managersSearch(centres, titulacions);
		model.addAllAttributes(map);
		
		session.setAttribute("respTitulacions", titulacions);
		session.setAttribute("respCentres", centres);
		
		return "components/list_managers";
	}
}