/*
 * Test the sys.process_empire_resources_updates() function
 */
BEGIN;
	/* We need:
	 *  - three types of resources,
	 *  - two empire with resources records, and the corresponding update
	 *    record,
	 *  - two planets, set to belong to the aforementioned empires, with
	 *    resources records attached. 
	 */
	\i utils/strings.sql
	\i utils/resources.sql
	\i utils/accounts.sql
	\i utils/naming.sql
	\i utils/universe.sql
	SELECT _create_resources( 3 , 'testResource' );
	SELECT _create_raw_planets( 2 , 'testPlanet' );
	SELECT _create_emp_names( 2 , 'testEmp' );
	SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet1' ) , 1 );
	INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
		SELECT _get_map_name( 'testPlanet1' ) , resource_name_id FROM defs.resources;
	INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
		SELECT _get_map_name( 'testPlanet2' ) , resource_name_id FROM defs.resources;

	/* Set up the planets' resource records so that:
	 *	- income > upkeep for testResource1 ,
	 *	- income < upkeep for testResource2 ,
	 *	- income = upkeep = 0 for testResource3 ,
	 */
	UPDATE verse.planet_resources SET pres_income = 12
		WHERE resource_name_id = _get_string( 'testResource1' );
	UPDATE verse.planet_resources SET pres_upkeep = 34
		WHERE resource_name_id = _get_string( 'testResource2' );

	/* Make sure update 0 is set to be processed for testEmp1 */
	UPDATE sys.updates
		SET status = 'PROCESSING' , last_tick = 0;

	/* Create testEmp2 empire, make sure it will not be processed */
	SELECT emp.create_empire( _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) , 1 );
	UPDATE sys.updates _su
		SET status = 'PROCESSED' , last_tick = 0
		FROM emp.updates _eu
			WHERE _eu.update_id = _su.id AND _eu.empire_id = _get_emp_name( 'testEmp2' );

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

	SELECT sys.process_empire_resources_updates( 10 );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (1/6)' );
	SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource1' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (2/6)' );
	SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource1' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (3/6)' );
	SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource2' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (4/6)' );
	SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource2' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (5/6)' );
	SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource3' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (6/6)' );
	SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource3' );

	SELECT sys.process_empire_resources_updates( 0 );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 1 - Possessed quantity increased' );
	SELECT is( empres_possessed , 12::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource1' )
			AND empire_id = _get_emp_name( 'testEmp1' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 1 - Owed quantity unchanged' );
	SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource1' )
			AND empire_id = _get_emp_name( 'testEmp1' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 2 - Possessed quantity unchanged' );
	SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource2' )
			AND empire_id = _get_emp_name( 'testEmp1' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 2 - Owed quantity increased' );
	SELECT is( empres_owed , 34::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource2' )
			AND empire_id = _get_emp_name( 'testEmp1' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 3 - Possessed quantity unchanged' );
	SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource3' )
			AND empire_id = _get_emp_name( 'testEmp1' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 3 - Owed quantity unchanged' );
	SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
		WHERE resource_name_id = _get_string( 'testResource3' )
			AND empire_id = _get_emp_name( 'testEmp1' );
	SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resources unchanged for already processed empire' );
	SELECT is( SUM( empres_owed + empres_possessed ) , 0::DOUBLE PRECISION )
		FROM emp.resources
		WHERE empire_id = _get_emp_name( 'testEmp2' );

	SELECT * FROM finish( );
ROLLBACK;