/*
 * 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: cannot test where the happiness column comes from until values
 *		  all use DOUBLE PRECISION.
 */

DELETE FROM sys.update_types
	WHERE updtype_name <> 'PlanetMining';

\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( 5 , 'empire' );
INSERT INTO emp.empires ( name_id , cash )
	SELECT id , 0 FROM naming.empire_names;

/* Planet #2 */
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 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 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 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 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;

/* Modify update records for the planets. Planet 6 is not to be processed.
 */
UPDATE sys.updates su
	SET update_state = 'PROCESSING' , update_last = 0
	FROM verse.planets_updates vu
	WHERE vu.update_id = su.update_id
		AND vu.name_id <>  _get_map_name( 'planet6' );
UPDATE sys.updates su
	SET update_state = 'PROCESSED' , update_last = 0
	FROM verse.planets_updates vu
	WHERE vu.update_id = su.update_id
		AND vu.name_id = _get_map_name( 'planet6' );