/*
 * Common setup for research update locking and empire selection
 * --------------------------------------------------------------
 *
 * Empires are selected when:
 *		- they are part of the current update batch,
 *		- they possess planets,
 *		- they have in-progress research entries.
 * All related data is locked: the update entries, empire, empire technology
 * records, technology definitions, planets (including ownership records and
 * happiness records), as well as the empire's name and associated account.
 */

-- Make sure the only update type left is EmpireResearch
DELETE FROM sys.update_types
	WHERE updtype_name <> 'EmpireResearch';

\i utils/strings.sql
\i utils/resources.sql
\i utils/accounts.sql
\i utils/naming.sql
\i utils/universe.sql

-- Create some technology definitions, dropping constraints on unused columns
ALTER TABLE defs.technologies
	ALTER technology_description_id DROP NOT NULL ,
	ALTER technology_discovery_id DROP NOT NULL ,
	ALTER technology_category_id DROP NOT NULL ,
	ALTER technology_price DROP NOT NULL;
SELECT _create_test_strings( 3 , 'tech' );
INSERT INTO defs.technologies ( technology_name_id , technology_points )
	SELECT id , 100.0 FROM defs.strings
		WHERE name LIKE 'tech%';

-- Create the empire names and planets
SELECT _create_emp_names( 6 , 'emp' );
SELECT _create_raw_planets( 4 , 'planet' );
INSERT INTO verse.planet_happiness( planet_id , target , current )
	SELECT id , 0.75 , 1500
		FROM naming.map_names
		WHERE name LIKE 'planet%';


/*
 * Empire 1 has no planets and no in-progress research. It will not be
 * selected or locked.
 */
INSERT INTO emp.empires( name_id , cash )
	VALUES( _get_emp_name( 'emp1' ) , 100.0 );
INSERT INTO emp.technologies (
		empire_id , technology_name_id ,
		emptech_state , emptech_points , emptech_priority )
	SELECT _get_emp_name( 'emp1' ) , technology_name_id , 'KNOWN' , NULL , NULL
		FROM defs.technologies;

/*
 * Empire 2 has planets, but no in-progress research. It will not be
 * selected.
 */
INSERT INTO emp.empires( name_id , cash )
	VALUES( _get_emp_name( 'emp2' ) , 100.0 );
INSERT INTO emp.technologies (
		empire_id , technology_name_id ,
		emptech_state , emptech_points , emptech_priority )
	SELECT _get_emp_name( 'emp2' ) , technology_name_id , 'KNOWN' , NULL , NULL
		FROM defs.technologies;
INSERT INTO emp.planets( empire_id , planet_id )
	VALUES( _get_emp_name( 'emp2' ) , _get_map_name( 'planet1' ) );

/*
 * Empire 3 has in-progress research, but no planets. It will not be selected.
 */
INSERT INTO emp.empires( name_id , cash )
	VALUES( _get_emp_name( 'emp3' ) , 100.0 );
INSERT INTO emp.technologies ( empire_id , technology_name_id )
	SELECT _get_emp_name( 'emp3' ) , technology_name_id
		FROM defs.technologies;

/*
 * Empire 4 has in-progress research and planets. It has no vacation mode
 * record. It will be selected.
 */
INSERT INTO emp.empires( name_id , cash )
	VALUES( _get_emp_name( 'emp4' ) , 100.0 );
INSERT INTO emp.technologies ( empire_id , technology_name_id )
	SELECT _get_emp_name( 'emp4' ) , technology_name_id
		FROM defs.technologies;
INSERT INTO emp.planets( empire_id , planet_id )
	VALUES( _get_emp_name( 'emp4' ) , _get_map_name( 'planet2' ) );

/*
 * Empire 5 has in-progress research, planets and a vacation mode record.
 * It will be selected.
 * 
 * Note: we need to add an entry to users.active_accounts for vacation mode
 * records to work.
 */
INSERT INTO emp.empires( name_id , cash )
	VALUES( _get_emp_name( 'emp5' ) , 100.0 );
INSERT INTO emp.technologies ( empire_id , technology_name_id )
	SELECT _get_emp_name( 'emp5' ) , technology_name_id
		FROM defs.technologies;
INSERT INTO emp.planets( empire_id , planet_id )
	VALUES( _get_emp_name( 'emp5' ) , _get_map_name( 'planet3' ) );
INSERT INTO users.active_accounts( credentials_id ,vacation_credits )
	SELECT id , 12
		FROM users.addresses
		WHERE address = 'emp5@example.org';
INSERT INTO users.vacations ( account_id , since , status )
	SELECT id , NOW() , 'PROCESSED'
		FROM users.addresses
		WHERE address = 'emp5@example.org';

/*
 * Empire 6 is similar to empire 4, but will not be marked for update in the
 * current batch.
 */
INSERT INTO emp.empires( name_id , cash )
	VALUES( _get_emp_name( 'emp6' ) , 100.0 );
INSERT INTO emp.technologies ( empire_id , technology_name_id )
	SELECT _get_emp_name( 'emp6' ) , technology_name_id
		FROM defs.technologies;
INSERT INTO emp.planets( empire_id , planet_id )
	VALUES( _get_emp_name( 'emp6' ) , _get_map_name( 'planet4' ) );


/* Speaking of current batch - set it up */
UPDATE sys.updates su
	SET update_state = 'PROCESSING' , update_last = 0
	FROM emp.empires_updates eu
	WHERE eu.update_id = su.update_id
		AND eu.name_id <>  _get_emp_name( 'emp6' );
UPDATE sys.updates su
	SET update_state = 'PROCESSED' , update_last = 0
	FROM emp.empires_updates eu
	WHERE eu.update_id = su.update_id
		AND eu.name_id =  _get_emp_name( 'emp6' );