<?php

/**
* Fired during plugin activation
*
* @link       http://www.exapartners.com/
* @since      1.0.0
*
* @package    Esi_Management
* @subpackage Esi_Management/includes
*/

/**
* Fired during plugin activation.
*
* This class defines all code necessary to run during the plugin's activation.
*
* @since      1.0.0
* @package    Esi_Management
* @subpackage Esi_Management/includes
* @author     Kremer Nicolas <nicolas.kremer@exapartners.com>
*/
class Esi_Management_Activator {

	/**
	* Short Description. (use period)
	*
	* Long Description.
	*
	* @since    1.0.0
	*/
	public static function activate() {

		global $wpdb;
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		$charset_collate = $wpdb->get_charset_collate();

		// Create Radar table
		$table_name = $wpdb->prefix . 'esi_radar';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Radar Industry liaison table
		$table_name = $wpdb->prefix . 'esi_radar_industry';
		$sql = "CREATE TABLE $table_name (
			radar_id INT NOT NULL,
			industry_id INT NOT NULL,
			PRIMARY KEY (radar_id, industry_id)
		)";
		dbDelta( $sql );


		// Create Industry table
		$table_name = $wpdb->prefix . 'esi_industry';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			parent INT(11) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Radar Function liaison table
		$table_name = $wpdb->prefix . 'esi_radar_function';
		$sql = "CREATE TABLE $table_name (
			radar_id INT NOT NULL,
			function_id INT NOT NULL,
			PRIMARY KEY (radar_id, function_id)
		)";
		dbDelta( $sql );

		// Create Function table
		$table_name = $wpdb->prefix . 'esi_function';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			parent INT(11) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create News table
		$table_name = $wpdb->prefix . 'esi_news';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			title varchar(255) NOT NULL,
			description text NOT NULL,
			url varchar(2083) NOT NULL,
			media_id INT(11) NOT NULL,
			company_id INT(11) NOT NULL,
			investment_id INT(11) NOT NULL,
			type_id INT(11) NOT NULL,
			region_id INT(11) NOT NULL,
			post_id BIGINT(20) NOT NULL,
			publish_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			record_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create News Radar liaison table
		$table_name = $wpdb->prefix . 'esi_news_radar';
		$sql = "CREATE TABLE $table_name (
			news_id INT NOT NULL,
			radar_id INT NOT NULL,
			PRIMARY KEY (news_id, radar_id)
		)";
		dbDelta( $sql );

		// Create News Industry liaison table
		$table_name = $wpdb->prefix . 'esi_news_industry';
		$sql = "CREATE TABLE $table_name (
			news_id INT NOT NULL,
			industry_id INT NOT NULL,
			PRIMARY KEY (news_id, industry_id)
		)";
		dbDelta( $sql );

		// Create News Function liaison table
		$table_name = $wpdb->prefix . 'esi_news_function';
		$sql = "CREATE TABLE $table_name (
			news_id INT NOT NULL,
			function_id INT NOT NULL,
			PRIMARY KEY (news_id, function_id)
		)";
		dbDelta( $sql );

		// Create Media table
		$table_name = $wpdb->prefix . 'esi_media';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			url varchar(2083) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Entity table
		$table_name = $wpdb->prefix . 'esi_entity';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			description text NOT NULL,
			url varchar(2083) NOT NULL,
			country varchar(2) NOT NULL,
			city varchar(255) NOT NULL,
			is_investor boolean NOT NULL,
			entity_type_id INT NOT NULL, 
			founded_in date DEFAULT '0000-00-00' NOT NULL,
			employees INT NOT NULL DEFAULT 0, 
			acquired_by INT NULL, 
			acquired_at date DEFAULT '0000-00-00',
			acquired_amount INT NULL,
			total_raised INT NULL,
			latest_round_type INT NULL
			
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Entity Type table
		$table_name = $wpdb->prefix . 'esi_entity_type';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			description text NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Entity Industry liaison table
		$table_name = $wpdb->prefix . 'esi_entity_industry';
		$sql = "CREATE TABLE $table_name (
			entity_id INT NOT NULL,
			industry_id INT NOT NULL,
			PRIMARY KEY (entity_id, industry_id)
		)";
		dbDelta( $sql );


		// Create Technology table
		$table_name = $wpdb->prefix . 'esi_technology';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			description text NOT NULL,
			parent INT(11) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Technology Entity liaison table
		$table_name = $wpdb->prefix . 'esi_technology_entity';
		$sql = "CREATE TABLE $table_name (
			technology_id INT NOT NULL,
			entity_id INT NOT NULL,
			PRIMARY KEY (technology_id, entity_id)
		)";

		dbDelta( $sql );

		// Create Technology News liaison table
		$table_name = $wpdb->prefix . 'esi_technology_news';
		$sql = "CREATE TABLE $table_name (
			technology_id INT NOT NULL,
			news_id INT NOT NULL,
			PRIMARY KEY (technology_id, news_id)
		)";
		dbDelta( $sql );

		// Create Ability table
		$table_name = $wpdb->prefix . 'esi_ability';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			description text NOT NULL,
			parent INT(11) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Ability Entity liaison table
		$table_name = $wpdb->prefix . 'esi_ability_entity';
		$sql = "CREATE TABLE $table_name (
			ability_id INT NOT NULL,
			entity_id INT NOT NULL,
			PRIMARY KEY (ability_id, entity_id)
		)";

		dbDelta( $sql );

		// Create Ability News liaison table
		$table_name = $wpdb->prefix . 'esi_ability_news';
		$sql = "CREATE TABLE $table_name (
			ability_id INT NOT NULL,
			news_id INT NOT NULL,
			PRIMARY KEY (ability_id, news_id)
		)";
		dbDelta( $sql );

		// Create Patent table
		$table_name = $wpdb->prefix . 'esi_patent';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			entity_id INT NOT NULL,
			code varchar(255) NOT NULL,
			description text NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Offices table
		$table_name = $wpdb->prefix . 'esi_offices';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			title varchar(255) NOT NULL,
			address varchar(255) NOT NULL,
			country varchar(2) NOT NULL,
			city varchar(255) NOT NULL,
			entity_id INT(11) NOT NULL,
			office_type_id INT(11) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create office type table
		$table_name = $wpdb->prefix . 'esi_office_type';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			description text NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Key Personnel table
		$table_name = $wpdb->prefix . 'esi_keypersonnel';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			firstname varchar(255) NOT NULL,
			lastname varchar(255) NOT NULL,
			entity_id INT(11) NOT NULL,
			personnel_role_id INT(11) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create personnel role table
		$table_name = $wpdb->prefix . 'esi_personnel_role';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			description text NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Type table
		$table_name = $wpdb->prefix . 'esi_type';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			description text NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Investment table
		$table_name = $wpdb->prefix . 'esi_investment';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			amount varchar(255) NOT NULL,
			investment_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			entity_id INT(11) NOT NULL,
			investment_type_id INT(11) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		$table_name = $wpdb->prefix . 'esi_investor';
		$sql = "CREATE TABLE $table_name (
			investment_id INT NOT NULL,
			entity_id INT NOT NULL,
			PRIMARY KEY (investment_id, entity_id)
		)";
		dbDelta( $sql );

		// Create Investment Type table
		$table_name = $wpdb->prefix . 'esi_investment_type';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			description text NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Investment Type table
		$table_name = $wpdb->prefix . 'esi_region';
		$sql = "CREATE TABLE $table_name (
			id INT NOT NULL AUTO_INCREMENT,
			name varchar(255) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

		// Create Country Table
		$table_name = $wpdb->prefix . 'esi_country';
		$sql = "CREATE TABLE $table_name (
			id INT(11) NOT NULL auto_increment,
			country_code varchar(2) NOT NULL,
			country_name varchar(100) NOT NULL,
			PRIMARY KEY (id)
		)";
		dbDelta( $sql );

	}

}
