/*
 * Utility functions used by unit tests
 *
 * Universe
 */

/*
 * Create a new system at some coordinates and return its identifier
 */
CREATE FUNCTION _create_system( INT , INT ) RETURNS INT AS $$
	INSERT INTO verse.systems ( x , y )
		VALUES ( $1 , $2 )
		RETURNING id;
$$ LANGUAGE SQL;


/*
 * Create "raw" planets
 */
CREATE FUNCTION _create_raw_planets( _quantity INT , _prefix TEXT )
		RETURNS VOID
	AS $$
DECLARE
	_system	INT;
	_orbit	INT;
	i		INT;
BEGIN
	PERFORM _create_map_names( _quantity , _prefix );
	
	i := 0;
	WHILE i < _quantity
	LOOP
		i := i + 1;
		
		IF _system IS NULL
		THEN
			_system := _create_system( i , i );
			_orbit := 1;
		END IF;
		
		INSERT INTO verse.planets(
			name_id , system_id , orbit , picture , population
		) VALUES (
			_get_map_name( _prefix || i::TEXT ) , _system , _orbit , 1 , 1
		);
		
		IF _orbit = 5
		THEN
			_system := NULL;
		ELSE
			_orbit := _orbit + 1;
		END IF;
	END LOOP;
END;
$$ LANGUAGE PLPGSQL;


/*
 * Create a resource provider
 */
CREATE FUNCTION _create_resource_provider( TEXT , TEXT ) RETURNS VOID AS $$
	INSERT INTO verse.resource_providers(
		planet_id , resource_name_id ,
		resprov_quantity_max , resprov_quantity ,
		resprov_difficulty , resprov_recovery
	) VALUES (
		_get_map_name( $1 ) , _get_string( $2 ) ,
		100 , 50 ,
		0.5 , 0.5
	);
$$ LANGUAGE SQL;