package it.enea.cruise.controller;

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

import javax.ws.rs.POST;

import it.enea.cruise.service.idp.IdpProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import it.enea.cruise.dto.ParametricQueryDTO;
import it.enea.cruise.dto.QueryParameterDTO;
import it.enea.cruise.model.parametricQuery.ParametricQuery;
import it.enea.cruise.model.user.User;
import it.enea.cruise.service.parametricQuery.LocalQueryParameterInterface;
import it.enea.cruise.service.parametricQuery.ParametricQueryInterface;
import it.enea.cruise.service.parametricQuery.ParametricQueryManager;
import it.enea.cruise.repositories.UserRepository;
import it.enea.cruise.model.parametricQuery.LocalQueryParameter;
import lombok.extern.log4j.Log4j2;

@RestController
@Api(value = "QueryController")
@EnableTransactionManagement
@Transactional("dataTransactionManagerIDP")
@Log4j2
public class QueryController {
	
	@Autowired
	LocalQueryParameterInterface lqpInterface;
	
	@Autowired
	ParametricQueryManager pqm;
	
	@Autowired
	ParametricQueryInterface pqueryInterface;
	
	@Autowired
	IdpProvider idpClientManager;
	
	@Autowired
    UserRepository userInterface;
	
	@Value("${platformRoleAdmin}")
	private String platformRoleAdmin;
	
	
	@POST
	@PostMapping(value = "/pq/getListOfQueriesData")
	public ResponseEntity<List<ParametricQuery>> getListOfQueriesData(
			@RequestHeader(name = "Authorization", required=false) String token,
			@RequestHeader(name = "Content-Language", required=false, defaultValue="it_IT") String language
			){
		
		List<ParametricQuery> data = pqueryInterface.findAll();
			
		return new ResponseEntity<List<ParametricQuery>>(data, HttpStatus.OK);
	}
	
	@POST
	@PostMapping(value = "/pq/newParametricQuery")
	public ResponseEntity<ParametricQueryDTO> newParametricQuery(
			@RequestHeader(name = "Authorization", required=false) String token,
			@RequestHeader(name = "Content-Language", required=false, defaultValue="it_IT") String language,
			@RequestBody ParametricQueryDTO pq
			){
		
		Boolean isValid = idpClientManager.isValid(token);
	
		User u = new User();
		
		if (isValid) {
			User userdto = idpClientManager.getUserByToken(token);
	
			u = userInterface.findBySubject(userdto.getUuid());
			if (u.hasRole(platformRoleAdmin)) {
				log.info(pq.toString());
		
				ParametricQuery pqNew = new ParametricQuery();
				pqNew.setSql(pq.getSql());
				pqNew.setQueryType("sql");
				List<LocalQueryParameter> localParameterList = new ArrayList<LocalQueryParameter>();
		
				for (QueryParameterDTO pDTO : pq.getParameters()) {
					LocalQueryParameter lpq = new LocalQueryParameter();
					lpq.setName(pDTO.getName());
					lpq.setDescription(pDTO.getDescription());
					lpq.setExample(pDTO.getExample());
					lpq.setFamilytype(pDTO.getFamilyType());
					lpq.setInout(pDTO.getInOut());
					lpq.setParamId(pDTO.getParameterId());
					lpq.setType(pDTO.getType());
					lpq.setUnitofmeasure(pDTO.getUnitOfMeasure());
			
					localParameterList.add(lpq);
			
				}
				pqNew.setParameters(localParameterList);
				
				pqueryInterface.save(pqNew);
		
			}
			return new ResponseEntity<ParametricQueryDTO>(pq, HttpStatus.OK);
		}
		
		return new ResponseEntity<ParametricQueryDTO>(new ParametricQueryDTO(), HttpStatus.FORBIDDEN);
	}


}
