/*
 * Test for the sys.process_planet_res_regen_updates() function
 */
BEGIN;

	/* 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
		);
	
	/* Insert update records for the planets. Planets 1 and 2 will be processed,
	 * planet testPlanet3 will not.
	 */
	INSERT INTO sys.updates ( id , gu_type , status , last_tick )
		VALUES ( 1 , 'PLANET_RES_REGEN' , 'PROCESSING' , 0 );
	INSERT INTO verse.updates ( update_id , planet_id )
		VALUES ( 1 , _get_map_name( 'testPlanet1' ) );

	INSERT INTO sys.updates ( id , gu_type , status , last_tick )
		VALUES ( 2 , 'PLANET_RES_REGEN' , 'PROCESSING' , 0 );
	INSERT INTO verse.updates ( update_id , planet_id )
		VALUES ( 2 , _get_map_name( 'testPlanet2' ) );

	INSERT INTO sys.updates ( id , gu_type , status , last_tick )
		VALUES ( 3 , 'PLANET_RES_REGEN' , 'PROCESSED' , 0 );
	INSERT INTO verse.updates ( update_id , planet_id )
		VALUES ( 3 , _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_planet_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_planet_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;