package es.uv.saic.web;

import java.io.IOException;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.annotation.Secured;
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 es.uv.saic.feign.ParseClient;
import jakarta.servlet.http.HttpServletResponse;

// Controller for the parsing options page for admin user
@Controller
public class ParseController {

	@Autowired
	private ParseClient pc;

	@Value("${saic.parser.surveys.path}")
	private String surveysPath;
	
	/*
	 * Parse a given table from a given database for a given year
	 * @param model
	 * @param auth Authentication
	 * @param response HttpServletResponse
	 * @param database The database to parse from
	 * @param table The table to parse from
	 * @param year The year to parse from
	 */
	@PostMapping("/admin/parse/do")
	@Secured({"ROLE_ADMIN"})
	public void doParse(Model model, Authentication auth, HttpServletResponse response, @RequestParam String database, 
			@RequestParam String table, @RequestParam Integer year) throws IOException, SQLException, ClassNotFoundException {		
		pc.doParse(response, database, table, year);
	}
	
	/*
	 * Parse table depending on the action requested: mediasCentroNP, bienalesProfesGrado, etc
	 * @param model
	 * @param auth Authentication
	 * @param response HttpServletResponse
	 * @param action The action to perform
	 */
	@PostMapping("/admin/parse/action")
	@Secured({"ROLE_ADMIN"})
	public void doAction(Model model, Authentication auth, HttpServletResponse response, @RequestParam String action) 
			throws IOException, SQLException, ClassNotFoundException {		
		pc.doAction(response, action);
	}
	
	/*
	 * Method to delete survey indicators from the database
	 * @param model
	 * @param auth Authentication
	 * @param response HttpServletResponse
	 * @param action The survey to delete indicators from
	 * @param year The year of the survey to delete indicators from (0 for all years)
	 */
	@PostMapping("/admin/parse/delete")
	@Secured({"ROLE_ADMIN"})
	public void doDelete(Model model, Authentication auth, HttpServletResponse response, @RequestParam String action, @RequestParam Integer year) 
			throws IOException, SQLException, ClassNotFoundException {		
		pc.doDelete(response, action, year);
	}
		
}
