/*
 * Test for the sys.process_planet_res_regen_updates() function
 */
BEGIN;
	/* Start by deleting all update types except for the one
	 * we're interested in.
	 */
	DELETE FROM sys.update_types
		WHERE updtype_name <> 'ResourceRegeneration';

	/* We need to create a natural resource and a pair of planets */
	\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 , 'testResource' );
	SELECT _create_raw_planets( 3 , 'testPlanet' );

	/* Define the necessary constants using default values */
	SELECT sys.uoc_constant( 'game.resources.recovery' , '(test)' , 'Resources' , 0.01 );
	SELECT sys.uoc_constant( 'game.resources.recoveryDampening' , '(test)' , 'Resources' , 1.5 );
	
	/* Create resource providers */
	INSERT INTO verse.resource_providers ( planet_id , resource_name_id ,
			resprov_quantity_max , resprov_quantity ,
			resprov_difficulty , resprov_recovery )
		VALUES (
			 _get_map_name( 'testPlanet1' ) ,  _get_string( 'testResource1' ) ,
			 100 , 50 ,
			 0.5 , 0.5
		) , (
			 _get_map_name( 'testPlanet2' ) ,  _get_string( 'testResource1' ) ,
			 100 , 100 ,
			 0.5 , 0.5
		) , (
			 _get_map_name( 'testPlanet3' ) ,  _get_string( 'testResource1' ) ,
			 100 , 50 ,
			 0.5 , 0.5
		);
	
	/* Modify update records for the planets. Planets 1 and 2 will be processed,
	 * planet testPlanet3 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( 'testPlanet3' );
	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( 'testPlanet3' );

	/* Helper function that gets a provider's current quantity */
	CREATE FUNCTION _get_quantity( _planet TEXT )
		RETURNS DOUBLE PRECISION
	AS $$
		SELECT resprov_quantity FROM verse.resource_providers
			WHERE planet_id = _get_map_name( $1 )
				AND resource_name_id = _get_string( 'testResource1' );
	$$ LANGUAGE SQL;

	/****** TESTS BEGIN HERE ******/
	SELECT plan( 6 );


	SELECT sys.process_res_regen_updates( 10 );
	SELECT diag_test_name( 'PLANET_RES_REGEN update - Wrong update identifier (1/3)' );
	SELECT ok( _get_quantity( 'testPlanet1' ) = 50 );
	SELECT diag_test_name( 'PLANET_RES_REGEN update - Wrong update identifier (2/3)' );
	SELECT ok( _get_quantity( 'testPlanet2' ) = 100 );
	SELECT diag_test_name( 'PLANET_RES_REGEN update - Wrong update identifier (3/3)' );
	SELECT ok( _get_quantity( 'testPlanet3' ) = 50 );


	SELECT sys.process_res_regen_updates( 0 );
	SELECT diag_test_name( 'PLANET_RES_REGEN update - Processed planet with quantity < max.' );
	SELECT ok( _get_quantity( 'testPlanet1' ) > 50 );
	SELECT diag_test_name( 'PLANET_RES_REGEN update - Processed planet with quantity = max.' );
	SELECT ok( _get_quantity( 'testPlanet2' ) = 100 );
	SELECT diag_test_name( 'PLANET_RES_REGEN update - Already processed planet' );
	SELECT ok( _get_quantity( 'testPlanet3' ) = 50 );


	SELECT * FROM finish( );
ROLLBACK;