package es.uv.saic.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

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.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.databind.ObjectMapper;

import es.uv.saic.domain.Usuari;
import es.uv.saic.dto.OrganDTO;
import es.uv.saic.feign.OrganClient;
import jakarta.servlet.http.HttpSession;

@Controller
public class OrganController {

	@Autowired
	private OrganClient oc;

	// POST que buscar titulaciones que se encuentren asociadas a uno o varios
	// centros
	@PostMapping("/search/titulations")
	public String getTitulationsByCenter(Model model, Authentication auth,
			@RequestParam(name = "centers[]", required = false) List<Integer> centres) throws IOException {
		HashMap<String, Object> response = oc.getTitulationsByCenter(centres,
				((Usuari) auth.getPrincipal()).getUsuari());

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_titulacions";
	}

	@PostMapping("/search/managers/titulations")
	public String getCenterTitulations(Model model, Authentication auth,
			@RequestParam("center") Integer centre,
			HttpSession session) {

		HashMap<String, Object> response = oc.getCenterTitulations(centre);

		if (response == null) {
			return "401";
		}

		List<Object> rawList = (List<Object>) response.get("resp_titulacions");
		ObjectMapper mapper = new ObjectMapper();

		List<OrganDTO> titulacions = rawList.stream()
				.map(obj -> mapper.convertValue(obj, OrganDTO.class))
				.collect(Collectors.toList());

		response.remove("resp_titulacions");

		model.addAllAttributes(response);
		model.addAttribute("sup_titulacions", titulacions);

		return "components/selector_titulacions_managers";
	}

	@PostMapping("/find/titulations2")
	public String getTitulationsByCenters(Model model, Authentication auth,
			@RequestParam("centers[]") List<Integer> centres, @RequestParam("procedure") Integer idProces)
			throws IOException {
		HashMap<String, Object> response = oc.getTitulationsByCenters(centres, idProces);

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_titulacions_managers";
	}

	@PostMapping("/find/titulations")
	public String getTitulationsByCenter(Model model, Authentication auth,
			@RequestParam("center") Integer centre, @RequestParam("procedure") Integer idProces) throws IOException {
		HashMap<String, Object> response = oc.getTitulationsByCenter(centre, idProces);

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_titulacions";
	}

	@PostMapping("/get/titulations")
	public String getAllTitulationsByCenter(Model model, Authentication auth, @RequestParam("center") Integer centre)
			throws IOException {
		HashMap<String, Object> response = oc.getAllTitulationsByCenter(centre);

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_titulacions";
	}

	@PostMapping("/find/centers")
	public String getAllCentresByAmbit(Model model, Authentication auth, @RequestParam("procedure") Integer idProces)
			throws IOException {
		HashMap<String, Object> response = oc.getAllCentresByAmbit(idProces);

		if (response == null) {
			return "401";
		}

		model.addAllAttributes(response);
		return "components/selector_centres";
	}
}
