/*
 * Tests for emp.resources_view
 */
BEGIN;

	/*
	 * We will use a fake emp.planet_resources_view to avoid having to set
	 * planet resources.
	 * 
	 * In terms of data, we need two resources (one with a category, the other
	 * without, and two empires (one with "planet resources", the other
	 * without). Both empires will have mining settings for one of the resource
	 * types.
	 */

	\i utils/strings.sql
	SELECT _create_test_strings( 2 , 'resource' , 'Resource name ' );
	SELECT _create_test_strings( 2 , 'rDesc' , 'Resource description ' );
	SELECT _create_test_strings( 1 , 'rCat' , 'Resource category ' );
	
	INSERT INTO defs.resources (
			resource_name_id , resource_description_id ,
			resource_category_id , resource_weight
		) VALUES (
			_get_string( 'resource1' ) , _get_string( 'rDesc1' ) ,
			_get_string( 'rCat1' ) , 2
		) , (
			_get_string( 'resource2' ) , _get_string( 'rDesc2' ) ,
			NULL , 1
		);

	\i utils/accounts.sql
	\i utils/naming.sql
	SELECT _create_emp_names( 2 , 'emp' );
	INSERT INTO emp.empires( name_id , cash )
		SELECT id , 0 FROM naming.empire_names; 

	ALTER TABLE emp.mining_settings DROP CONSTRAINT fk_empmset_resource;
	INSERT INTO emp.mining_settings ( empire_id , resource_name_id , empmset_weight )
		SELECT id , _get_string( 'resource1' ) , row_number( ) OVER ()
			FROM naming.empire_names
			ORDER BY id;

	INSERT INTO emp.resources ( empire_id , resource_name_id , empres_possessed )
		SELECT name_id , resource_name_id , 0.4 * row_number( ) OVER ()
			FROM emp.empires
				CROSS JOIN defs.resources
			ORDER BY name_id , resource_name_id;


	CREATE TABLE fake_planet_resources_view (
		empire_id			INT ,
		resource_name_id	INT ,
		planets_income		BIGINT ,
		planets_upkeep		BIGINT
	);
	
	CREATE OR REPLACE VIEW emp.planet_resources_view
		AS SELECT * FROM fake_planet_resources_view;

	INSERT INTO fake_planet_resources_view
		VALUES (
			_get_emp_name( 'emp1' ) , _get_string( 'resource1' ) , 1 , 2
		) , (
			_get_emp_name( 'emp1' ) , _get_string( 'resource2' ) , 3 , 4
		);


	/***** TESTS BEGIN HERE *****/
	SELECT plan( 13 );
	
	SELECT diag_test_name( 'emp.resources_view - One row per empire/resource combination' );
	SELECT is( COUNT(*)::INT , 4 ) FROM emp.resources_view;
	
	SELECT diag_test_name( 'emp.resources_view - Resource ordering' );
	SELECT set_eq(
		$$ SELECT resource_identifier , row_number( ) OVER ( )
			FROM emp.resources_view 
			WHERE empire_id = _get_emp_name( 'emp1' ) $$ ,
		$$ VALUES ( 'resource2' , 1 ) , ( 'resource1' , 2 ) $$
	);
	
	SELECT diag_test_name( 'emp.resources_view - Name translation' );
	SELECT is_empty( $$
		SELECT *
			FROM emp.resources_view
			WHERE resource_name NOT LIKE 'Resource name %'
	$$ );
	
	SELECT diag_test_name( 'emp.resources_view - Description translation' );
	SELECT is_empty( $$
		SELECT *
			FROM emp.resources_view
			WHERE resource_description NOT LIKE 'Resource description %'
	$$ );
	
	SELECT diag_test_name( 'emp.resources_view - Category translation' );
	SELECT is_empty( $$
		SELECT *
			FROM emp.resources_view
			WHERE resource_identifier = 'resource1'
				AND resource_category NOT LIKE 'Resource category %'
	$$ );
	
	SELECT diag_test_name( 'emp.resources_view - NULL category -> NULL translation' );
	SELECT is_empty( $$
		SELECT *
			FROM emp.resources_view
			WHERE resource_identifier <> 'resource1'
				AND resource_category IS NOT NULL
	$$ );
	
	SELECT diag_test_name( 'emp.resources_view - Possessed quantities are rounded down' );
	SELECT set_eq(
		$$ SELECT empire_id , resource_identifier , empres_possessed
			FROM emp.resources_view $$ ,
		$$ VALUES (
				_get_emp_name( 'emp1' ) , 'resource1' , 0
			) , (
				_get_emp_name( 'emp1' ) , 'resource2' , 0
			) , (
				_get_emp_name( 'emp2' ) , 'resource1' , 1
			) , (
				_get_emp_name( 'emp2' ) , 'resource2' , 1
			) $$
	);
	
	SELECT diag_test_name( 'emp.resources_view - Basic resources have NULL mining settings' );
	SELECT is( COUNT(*)::INT , 2 )
		FROM emp.resources_view
		WHERE resource_identifier = 'resource2'
			AND empmset_weight IS NULL;
	
	SELECT diag_test_name( 'emp.resources_view - Mining settings for natural resources' );
	SELECT set_eq(
		$$ SELECT empire_id , empmset_weight
			FROM emp.resources_view
			WHERE resource_identifier = 'resource1' $$ ,
		$$ VALUES ( _get_emp_name( 'emp1' ) , 1 ) ,
			( _get_emp_name( 'emp2' ) , 2 ) $$
	);
	SELECT is( COUNT(*)::INT , 2 )
		FROM emp.resources_view
		WHERE resource_identifier = 'resource1'
			AND empmset_weight IS NOT NULL;
	
	SELECT diag_test_name( 'emp.resources_view - Planet upkeep/income is zero when there are no planets' );
	SELECT is( COUNT(*)::INT , 2 )
		FROM emp.resources_view
		WHERE empire_id = _get_emp_name( 'emp2' )
			AND planets_upkeep = 0
			AND planets_income = 0;
	
	SELECT diag_test_name( 'emp.resources_view - Planet upkeep/income from planet resources view' );
	SELECT set_eq(
		$$ SELECT resource_identifier , planets_upkeep , planets_income
			FROM emp.resources_view
			WHERE empire_id = _get_emp_name( 'emp1' ) $$ ,
		$$ VALUES ( 'resource1' , 2 , 1 ) , ( 'resource2' , 4 , 3 ) $$
	);
	
	SELECT diag_test_name( 'emp.resources_view - FIXME - Fleets upkeep set to zero' );
	SELECT is_empty(
		$$ SELECT * FROM emp.resources_view
			WHERE fleets_upkeep <> 0 $$
	);

	SELECT * FROM finish( );
ROLLBACK;