/*
 * We need rows in quite a few tables to make sure locking works as
 * advertised.
 *
 * - First we will need a planet with no resource providers. This one
 *   shouldn't be selected at all.
 * 
 * - We need a planet with resource providers, but no owning empire. The
 *   planet will be selected, but all fields that are obtained from the
 *   empire's data will be NULL.
 *
 * - We need a planet owned by an empire for which empire-wide settings
 *   will be used.
 *
 * - We need a planet owned by an empire but that uses planet-specific
 *   settings.
 *
 * - We need a planet with no resource providers owned by an empire. This
 *   one shouldn't be selected.
 *
 * - Finally, we need a planet that matches the criterias but isn't
 *   scheduled for an update at the time. 
 *
 * FIXME: for now, locking is NOT tested. I simply have no idea how to do
 *        it, at least not without creating an extra database and that
 *        kind of stuff.
 *
 * FIXME: cannot test where the happiness column comes from until values
 *		  all use DOUBLE PRECISION.
 */
\i utils/strings.sql
\i utils/resources.sql
\i utils/accounts.sql
\i utils/naming.sql
\i utils/universe.sql

SELECT sys.uoc_constant( 'game.resources.weightBase' , '(test)' , 'Resources' , 10.0 );
SELECT _create_natural_resources( 2 , 'resource' );
SELECT _create_raw_planets( 6 , 'planet' );
SELECT _create_emp_names( 4 , 'empire' );
INSERT INTO emp.empires ( name_id , cash )
	SELECT id , 0 FROM naming.empire_names;

/* Planet #1 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
	VALUES ( 1 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
	VALUES ( 1 , _get_map_name( 'planet1' ) );

/* Planet #2 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
	VALUES ( 2 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
	VALUES ( 2 , _get_map_name( 'planet2' ) );
INSERT INTO verse.resource_providers (
		planet_id , resource_name_id , resprov_quantity_max ,
		resprov_quantity , resprov_difficulty , resprov_recovery
	) VALUES (
		_get_map_name( 'planet2' ) , _get_string( 'resource1' ) , 100 ,
		100 , 0.2 , 0.5
	) , (
		_get_map_name( 'planet2' ) , _get_string( 'resource2' ) , 100 ,
		100 , 0.2 , 0.5
	);

/* Planet #3 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
	VALUES ( 3 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
	VALUES ( 3 , _get_map_name( 'planet3' ) );
INSERT INTO verse.resource_providers (
		planet_id , resource_name_id , resprov_quantity_max ,
		resprov_quantity , resprov_difficulty , resprov_recovery
	) VALUES (
		_get_map_name( 'planet3' ) , _get_string( 'resource1' ) , 100 ,
		100 , 0.3 , 0.5
	) , (
		_get_map_name( 'planet3' ) , _get_string( 'resource2' ) , 100 ,
		100 , 0.3 , 0.5
	);
INSERT INTO verse.planet_happiness ( planet_id , current , target )
	VALUES ( _get_map_name( 'planet3' ) , 0.3 , 0.3 );
INSERT INTO emp.planets ( empire_id , planet_id )
	VALUES ( _get_emp_name( 'empire1' ) , _get_map_name( 'planet3' ) );
INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight )
	VALUES (
		_get_emp_name( 'empire1' ) , _get_string( 'resource1' ) , 2
	) , (
		_get_emp_name( 'empire1' ) , _get_string( 'resource2' ) , 2
	);

/* Planet #4 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
	VALUES ( 4 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
	VALUES ( 4 , _get_map_name( 'planet4' ) );
INSERT INTO verse.resource_providers (
		planet_id , resource_name_id , resprov_quantity_max ,
		resprov_quantity , resprov_difficulty , resprov_recovery
	) VALUES (
		_get_map_name( 'planet4' ) , _get_string( 'resource1' ) , 100 ,
		100 , 0.4 , 0.5
	) , (
		_get_map_name( 'planet4' ) , _get_string( 'resource2' ) , 100 ,
		100 , 0.4 , 0.5
	);
INSERT INTO verse.planet_happiness ( planet_id , current , target )
	VALUES ( _get_map_name( 'planet4' ) , 0.4 , 0.4 );
INSERT INTO emp.planets ( empire_id , planet_id )
	VALUES ( _get_emp_name( 'empire2' ) , _get_map_name( 'planet4' ) );
INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight )
	VALUES (
		_get_emp_name( 'empire2' ) , _get_string( 'resource1' ) , 2
	) , (
		_get_emp_name( 'empire2' ) , _get_string( 'resource2' ) , 2
	);
INSERT INTO emp.planet_mining_settings(
		empire_id , planet_id , resource_name_id , emppmset_weight
	) VALUES (
			_get_emp_name( 'empire2' ) , _get_map_name( 'planet4' ) , _get_string( 'resource1' ) , 1
		) , (
			_get_emp_name( 'empire2' ) , _get_map_name( 'planet4' ) , _get_string( 'resource2' ) , 3
		);

/* Planet #5 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
	VALUES ( 5 , 'PLANET_MINING' , 'PROCESSING' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
	VALUES ( 5 , _get_map_name( 'planet5' ) );
INSERT INTO verse.planet_happiness ( planet_id , current , target )
	VALUES ( _get_map_name( 'planet5' ) , 0.5 , 0.5 );
INSERT INTO emp.planets ( empire_id , planet_id )
	VALUES ( _get_emp_name( 'empire3' ) , _get_map_name( 'planet5' ) );
INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight )
	VALUES (
		_get_emp_name( 'empire3' ) , _get_string( 'resource1' ) , 2
	) , (
		_get_emp_name( 'empire3' ) , _get_string( 'resource2' ) , 2
	);

/* Planet #6 */
INSERT INTO sys.updates( id , gu_type , status , last_tick )
	VALUES ( 6 , 'PLANET_MINING' , 'PROCESSED' , 0 );
INSERT INTO verse.updates( update_id , planet_id )
	VALUES ( 6 , _get_map_name( 'planet6' ) );
INSERT INTO verse.resource_providers (
		planet_id , resource_name_id , resprov_quantity_max ,
		resprov_quantity , resprov_difficulty , resprov_recovery
	) VALUES (
		_get_map_name( 'planet6' ) , _get_string( 'resource1' ) , 100 ,
		100 , 0.6 , 0.5
	) , (
		_get_map_name( 'planet6' ) , _get_string( 'resource2' ) , 100 ,
		100 , 0.6 , 0.5
	);
INSERT INTO verse.planet_happiness ( planet_id , current , target )
	VALUES ( _get_map_name( 'planet6' ) , 0.6 , 0.6 );
INSERT INTO emp.planets ( empire_id , planet_id )
	VALUES ( _get_emp_name( 'empire4' ) , _get_map_name( 'planet6' ) );
INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight )
	VALUES (
		_get_emp_name( 'empire4' ) , _get_string( 'resource1' ) , 2
	) , (
		_get_emp_name( 'empire4' ) , _get_string( 'resource2' ) , 2
	);

/* Insert planet resource records */
INSERT INTO verse.planet_resources ( planet_id , resource_name_id , pres_income , pres_upkeep )
	SELECT p.name_id , r.resource_name_id , 42 , 0
		FROM verse.planets p CROSS JOIN defs.resources r;
