/*
This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course.
© Copyright Utrecht University (Department of Information and Computing Sciences)
*/

package aql

import (
	"errors"
	"fmt"

	"git.science.uu.nl/graphpolaris/query-conversion/entity"
)

// Version 1.13

/*
ConvertQuery converts an IncomingQueryJSON object into AQL
	JSONQuery: *entity.IncomingQueryJSON, the query to be converted to AQL
	Returns: *string, the AQL query and a possible error
*/
func (s *Service) ConvertQuery(JSONQuery *entity.IncomingQueryJSON) (*string, error) {

	// Check to make sure all indexes exist
	// The largest possible id for an entity
	largestEntityID := len(JSONQuery.Entities) - 1
	// The largest possible id for a relation
	largestRelationID := len(JSONQuery.Relations) - 1

	// Make sure no entity should be returned that is outside the range of that list
	for _, e := range JSONQuery.Return.Entities {
		// If this entity references an entity that is outside the range
		if e > largestEntityID || e < 0 {
			return nil, errors.New("non-existing entity referenced in return")
		}
	}

	// Make sure that no relation mentions a non-existing entity
	for _, r := range JSONQuery.Relations {
		if r.EntityFrom > largestEntityID || r.EntityTo > largestEntityID {
			return nil, errors.New("non-exisiting entity referenced in relation")
		}
	}

	// Make sure no non-existing relation is tried to be returned
	for _, r := range JSONQuery.Return.Relations {
		if r > largestRelationID || r < 0 {
			return nil, errors.New("non-existing relation referenced in return")
		}
	}
	result := createQuery(JSONQuery)
	return result, nil
}

/*
createQuery generates a query based on the json file provided
	JSONQuery: *entity.IncomingQueryJSON, this is a parsedJSON struct holding all the data needed to form a query,
	Return: *string, a string containing the corresponding AQL query and an error
*/
func createQuery(JSONQuery *entity.IncomingQueryJSON) *string {
	query := ""
	for list := range listoflists {
		for index := range listoflists[list] {
			element := listoflists[list][index]
			switch element.typename {
			case "entity":
				entity := JSONQuery.Entities[element.pointer]
				query += entityToQuery(entity)
			case "relation":
				relation := JSONQuery.Relations[element.pointer]
				query += relationToQuery(relation)
			case "function":
				function := JSONQuery.GroupBys[element.pointer]
				query += functionToQuery(function)
			case "filter":
				filter := JSONQuery.Filters[element.pointer]
				query += filterToQuery(filter)
			}
		}
	}
}

func entityToQuery(element entity.QueryEntityStruct) string {
	thisname := fmt.Sprintf("e%v", element.ID)
	ret := createLetFor(thisname, element.Name)
	ret += "RETURN x\n)"
	return ret
}

func relationToQuery(element entity.QueryRelationStruct) string {
	thisname := fmt.Sprintf("e%v", element.ID)
	ret := createLetFor(thisname, element.Name)
	return ret
}

func functionToQuery(element entity.QueryGroupByStruct) string {
	thisname := fmt.Sprintf("e%v", element.ID)
	element
	ret := createLetFor(thisname)
	return ret
}

func filterToQuery(element entity.QueryFilterStruct) string {
	thisname := fmt.Sprintf("e%v", element.ID)
	ret := createLetFor(thisname, fmt.Sprintf("e%v", element.FilteredID))
	return ret
}

func createLetFor(variableName string, enumerableName string) string {
	return "LET " + variableName + " = (\n\tFOR x IN " + enumerableName + "\n"
}