/*
 * Test the emp.create_empire() function
 */
BEGIN;
	/* We need a pair of natural resources, a basic resource, a planet
	 * and an empire name. We add a resource provider to the planet.
	 */
	\i utils/strings.sql
	\i utils/resources.sql
	\i utils/accounts.sql
	\i utils/naming.sql
	\i utils/universe.sql
	SELECT _create_natural_resources( 2 , 'natRes' );
	SELECT _create_resources( 1 , 'basicRes' );
	SELECT _create_raw_planets( 1 , 'testPlanet' );
	SELECT _create_emp_names( 1 , 'testEmp' );
	
	INSERT INTO verse.planet_resources( planet_id , resource_name_id )
		SELECT name_id , resource_name_id
			FROM verse.planets CROSS JOIN defs.resources;

	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( 'natRes1' ) , 100 ,
			100 , 0.2 , 0.5
		);

	/*
	 * We also need 3 technologies (tech1, tech2 and tech3), and a chain of
	 * dependencies between them (i.e. tech3 -> tech2 -> tech1). Disabling
	 * unused fields in defs.technologies makes things easier.
	 */
	ALTER TABLE defs.technologies
		ALTER technology_category_id DROP NOT NULL ,
		ALTER technology_discovery_id DROP NOT NULL ,
		ALTER technology_description_id DROP NOT NULL ,
		ALTER technology_price DROP NOT NULL ,
		ALTER technology_points DROP NOT NULL;
	SELECT _create_test_strings( 3 , 'tech' );
	INSERT INTO defs.technologies ( technology_name_id )
		VALUES ( _get_string( 'tech1' ) ) ,
				( _get_string( 'tech2' ) ) ,
				( _get_string( 'tech3' ) );
	INSERT INTO defs.technology_dependencies(
			technology_name_id , technology_name_id_depends
		) VALUES ( _get_string( 'tech2' ) , _get_string( 'tech1' ) ) ,
			 ( _get_string( 'tech3' ) , _get_string( 'tech2' ) );


	/* We replace the emp.mining_get_input() and emp.mining_compute_extraction()
	 * functions with something we control fully.
	 */
	CREATE OR REPLACE FUNCTION emp.mining_get_input( _empire INT )
		RETURNS SETOF emp.planet_mining_type
		LANGUAGE SQL
	AS $$
		SELECT _get_map_name( 'testPlanet1' ) AS planet ,
			_get_string( 'natRes1' ) 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( 9 );
	
	SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) ,
				_get_map_name( 'testPlanet1' ) ,
				200.0 );

	SELECT diag_test_name( 'emp.create_empire() - Empire exists' );
	SELECT is( COUNT(*)::INT , 1 ) FROM emp.empires
		WHERE name_id = _get_emp_name( 'testEmp1' );

	SELECT diag_test_name( 'emp.create_empire() - Empire cash set' );
	SELECT is( cash , 200.0::REAL ) FROM emp.empires
		WHERE name_id = _get_emp_name( 'testEmp1' );

	SELECT diag_test_name( 'emp.create_empire() - Empire debt set' );
	SELECT is( debt , 0.0::REAL ) FROM emp.empires
		WHERE name_id = _get_emp_name( 'testEmp1' );

	SELECT diag_test_name( 'emp.create_empire() - Empire mining settings include all natural resources' );
	SELECT is( COUNT(*)::INT , 2 )
		FROM defs.natural_resources
			INNER JOIN emp.mining_settings
				USING ( resource_name_id )
		WHERE empire_id = _get_emp_name( 'testEmp1' );

	SELECT diag_test_name( 'emp.create_empire() - Empire mining settings do not include basic resources' );
	SELECT is( COUNT(*)::INT , 2 )
		FROM defs.resources
			INNER JOIN emp.mining_settings
				USING ( resource_name_id )
		WHERE empire_id = _get_emp_name( 'testEmp1' );

	SELECT diag_test_name( 'emp.create_empire() - Empire mining settings are all set to 2' );
	SELECT is( COUNT( * )::INT , 0 )
		FROM emp.mining_settings
		WHERE empire_id = _get_emp_name( 'testEmp1' )
			AND empmset_weight <> 2;

	SELECT diag_test_name( 'emp.create_empire() - Empire resources have been initialised' );
	SELECT is( COUNT(*)::INT , 3 )
		FROM emp.resources
		WHERE empire_id = _get_emp_name( 'testEmp1' );

	SELECT diag_test_name( 'emp.create_empire() - Empire technologies have been initialised' );
	SELECT set_eq( $$
		SELECT technology_name_id , emptech_state::TEXT , emptech_points , emptech_priority
			FROM emp.technologies_v2
			WHERE empire_id = _get_emp_name( 'testEmp1' );
	$$ , $$ VALUES(
		_get_string( 'tech1' ) , 'RESEARCH' , 0.0 , 2
	) $$ );

	SELECT diag_test_name( 'emp.create_empire() - Resource mining has been updated' );
	SELECT is( pres_income::DOUBLE PRECISION , 42::DOUBLE PRECISION )
		FROM verse.planet_resources
		WHERE planet_id = _get_map_name( 'testPlanet1' )
			AND resource_name_id = _get_string( 'natRes1' );

	SELECT * FROM finish( );
ROLLBACK;