/*
 * Unit tests for emp.technology_implement()
 */
BEGIN;
	\i utils/strings.sql
	\i utils/resources.sql
	\i utils/accounts.sql
	\i utils/naming.sql
	\i utils/universe.sql

	/*
	 * Create empires
	 */
	SELECT _create_emp_names( 4 , 'emp' );
	INSERT INTO emp.empires ( name_id , cash )
		SELECT id , 200 FROM naming.empire_names;

	/*
	 * We also need 3 technologies (tech1, tech2 and tech3), and some
	 * dependencies between them: 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_points DROP NOT NULL;
	SELECT _create_test_strings( 3 , 'tech' );
	INSERT INTO defs.technologies ( technology_name_id , technology_price )
		VALUES ( _get_string( 'tech1' ) , 200 ) ,
				( _get_string( 'tech2' ) , 200 ) ,
				( _get_string( 'tech3' ) , 300 );
	INSERT INTO defs.technology_dependencies(
			technology_name_id , technology_name_id_depends
		) VALUES ( _get_string( 'tech3' ) , _get_string( 'tech1' ) ) ,
			 ( _get_string( 'tech3' ) , _get_string( 'tech2' ) );

	/* Empire "emp1" has only in-progress research. */
	INSERT INTO emp.technologies_v2 ( empire_id , technology_name_id )
		VALUES( _get_emp_name( 'emp1' ) , _get_string( 'tech1' ) );

	/* Empire "emp2" has a pending technology. */
	INSERT INTO emp.technologies_v2 ( empire_id , technology_name_id , emptech_state , emptech_points , emptech_priority )
		VALUES( _get_emp_name( 'emp2' ) , _get_string( 'tech1' ) , 'PENDING' , NULL , NULL );

	/* Empire "emp3" has implemented 'tech1' and has 'tech2' as pending. */
	INSERT INTO emp.technologies_v2 ( empire_id , technology_name_id , emptech_state , emptech_points , emptech_priority )
		VALUES( _get_emp_name( 'emp3' ) , _get_string( 'tech1' ) , 'KNOWN' , NULL , NULL ) ,
			( _get_emp_name( 'emp3' ) , _get_string( 'tech2' ) , 'PENDING' , NULL , NULL );

	/* Empire "emp4" has implemented 'tech1' and 'tech2' and has 'tech3' as pending. */
	INSERT INTO emp.technologies_v2 ( empire_id , technology_name_id , emptech_state , emptech_points , emptech_priority )
		VALUES( _get_emp_name( 'emp4' ) , _get_string( 'tech1' ) , 'KNOWN' , NULL , NULL ) ,
			( _get_emp_name( 'emp4' ) , _get_string( 'tech2' ) , 'KNOWN' , NULL , NULL ) ,
			( _get_emp_name( 'emp4' ) , _get_string( 'tech3' ) , 'PENDING' , NULL , NULL );

	-- ***** TESTS BEGIN HERE *****
	SELECT plan( 10 );

	SELECT diag_test_name( 'emp.technology_implement() - Call on in-progress research' );
	SELECT ok( NOT emp.technology_implement( _get_emp_name( 'emp1' ) , 'tech1' ) );

	SELECT diag_test_name( 'emp.technology_implement() - Call on unknown technology' );
	SELECT ok( NOT emp.technology_implement( _get_emp_name( 'emp1' ) , 'tech2' ) );

	SELECT diag_test_name( 'emp.technology_implement() - Call on implemented technology' );
	SELECT ok( NOT emp.technology_implement( _get_emp_name( 'emp3' ) , 'tech1' ) );

	SELECT diag_test_name( 'emp.technology_implement() - Call on pending technology - No new research - Return value' );
	SELECT ok( emp.technology_implement( _get_emp_name( 'emp2' ) , 'tech1' ) );

	SELECT diag_test_name( 'emp.technology_implement() - Call on pending technology - No new research - Table contents' );
	SELECT set_eq( $$
		SELECT technology_name_id , emptech_state::TEXT
			FROM emp.technologies_v2
			WHERE empire_id = _get_emp_name( 'emp2' )
	$$ , $$ VALUES(
		_get_string( 'tech1' ) , 'KNOWN'
	) $$ );

	SELECT diag_test_name( 'emp.technology_implement() - Call on pending technology - No new research - Empire cash' );
	SELECT is( cash , 0.0::REAL ) FROM emp.empires
		WHERE name_id = _get_emp_name( 'emp2' );

	SELECT diag_test_name( 'emp.technology_implement() - Call on pending technology - New research - Return value' );
	SELECT ok( emp.technology_implement( _get_emp_name( 'emp3' ) , 'tech2' ) );

	SELECT diag_test_name( 'emp.technology_implement() - Call on pending technology - New research - Table contents' );
	SELECT set_eq( $$
		SELECT technology_name_id , emptech_state::TEXT
			FROM emp.technologies_v2
			WHERE empire_id = _get_emp_name( 'emp3' )
	$$ , $$ VALUES(
		_get_string( 'tech1' ) , 'KNOWN'
	) , (
		_get_string( 'tech2' ) , 'KNOWN'
	) , (
		_get_string( 'tech3' ) , 'RESEARCH'
	) $$ );

	SELECT diag_test_name( 'emp.technology_implement() - Call on pending technology - New research - Empire cash' );
	SELECT is( cash , 0.0::REAL ) FROM emp.empires
		WHERE name_id = _get_emp_name( 'emp3' );


	SELECT diag_test_name( 'emp.technology_implement() - Call on pending technology when empire cash is too low' );
	SELECT ok( NOT emp.technology_implement( _get_emp_name( 'emp4' ) , 'tech3' ) );

	SELECT * FROM finish( );
ROLLBACK;