/*
 * Test the sys.process_planet_mining_updates() function
 */
BEGIN;
	/* Disable other update types */
	DELETE FROM sys.update_types
		WHERE updtype_name <> 'PlanetMining';

	/* We need two planets with identicals resource records and resource providers. */
	\i utils/strings.sql
	\i utils/resources.sql
	\i utils/accounts.sql
	\i utils/naming.sql
	\i utils/universe.sql

	SELECT _create_natural_resources( 1 , 'resource' );
	SELECT _create_raw_planets( 2 , 'planet' );

	INSERT INTO verse.resource_providers (
			planet_id , resource_name_id , resprov_quantity_max ,
			resprov_quantity , resprov_difficulty , resprov_recovery
		) VALUES (
			_get_map_name( 'planet1' ) , _get_string( 'resource1' ) , 100 ,
			100 , 0.2 , 0.5
		) , (
			_get_map_name( 'planet2' ) , _get_string( 'resource1' ) , 100 ,
			100 , 0.2 , 0.5
		);

	INSERT INTO verse.planet_resources ( planet_id , resource_name_id , pres_income , pres_upkeep )
		SELECT p.name_id , r.resource_name_id , 50 , 0
			FROM verse.planets p CROSS JOIN defs.resources r;

	/* First planet will be updated, second will not */
	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( 'planet1' );
	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( 'planet2' );

	/* Replace sys.gu_pmc_get_data() and emp.mining_compute_extraction(); both
	 * functions will write to temporary tables. 
	 */
	CREATE TABLE _get_data_calls( tick BIGINT );
	CREATE OR REPLACE FUNCTION sys.gu_pmc_get_data( _tick BIGINT )
		RETURNS SETOF emp.planet_mining_type LANGUAGE SQL
	AS $$
		INSERT INTO _get_data_calls VALUES ( $1 );
		SELECT _get_map_name( 'planet1' ) AS planet ,
			_get_string( 'resource1' ) AS resource ,
			NULL::DOUBLE PRECISION AS quantity , NULL::DOUBLE PRECISION AS quantity_max ,
			NULL::DOUBLE PRECISION AS difficulty , NULL::INT AS empire ,
			NULL::REAL AS happiness , NULL::DOUBLE PRECISION AS weight ,
			NULL::DOUBLE PRECISION AS total_weight;
	$$;
	
	CREATE OR REPLACE FUNCTION emp.mining_compute_extraction( _input emp.planet_mining_type )
			RETURNS DOUBLE PRECISION LANGUAGE SQL
	AS $$
		SELECT 42::DOUBLE PRECISION;
	$$;

	/***** TESTS BEGIN HERE *****/
	SELECT plan( 3 );
	
	SELECT sys.process_planet_mining_updates( 0 );

	SELECT diag_test_name( 'sys.process_planet_mining_updates() - Resource providers updated' );
	SELECT set_eq(
		$$ SELECT planet_id , resprov_quantity FROM verse.resource_providers $$ ,
		$$ VALUES ( _get_map_name( 'planet1' ) , 50 ) , ( _get_map_name( 'planet2' ) , 100 ) $$
	);

	SELECT diag_test_name( 'sys.process_planet_mining_updates() - Planet resources updated' );
	SELECT set_eq(
		$$ SELECT planet_id , pres_income FROM verse.planet_resources $$ ,
		$$ VALUES ( _get_map_name( 'planet1' ) , 42 ) , ( _get_map_name( 'planet2' ) , 50 ) $$
	);

	SELECT diag_test_name( 'sys.process_planet_mining_updates() - Two calls to gu_pmc_get_data()' );
	SELECT is( COUNT(*)::INT , 2 ) FROM _get_data_calls;

	SELECT * FROM finish( );
ROLLBACK;
