<?php

/**
 * General Wiaas DB updates
 *
 * General functions for updating wiaas database to latest version
 */

function wiaas_db_update_setup_gravity() {
	// Gravity Form settings
	update_option('gform_pending_installation', false);
	update_option( 'gform_enable_background_updates', false );
	update_option('rg_gforms_enable_akismet', 0);
	update_option('rg_gforms_currency', 'USD');

	// Gravity Flow settings
	update_option('gravityflow_pending_installation', false);
}

function wiaas_db_update_enable_orders_access_management() {
	$post_types_option = Groups_Options::get_option( Groups_Post_Access::POST_TYPES, array() );

	$post_types_option['shop_order'] = array(
		'add_meta_box' => true
	);

	Groups_Options::update_option(Groups_Post_Access::POST_TYPES, $post_types_option);
}

function wiaas_db_setup_exclusive_taxonomies() {
	update_option('radio_button_for_taxonomies_options', array(
		'taxonomies' => array(
			'shop_order_project',
		),
		'delete' => 0,
	));
}

function wiaas_db_setup_customer_capabilities() {
	$customer_role = get_role('customer');

	$customer_role->add_cap('read_private_shop_orders');
	$customer_role->add_cap('read_private_products');
	$customer_role->add_cap('read_shop_order');
	$customer_role->add_cap('publish_shop_orders');

	// user
	$customer_role->add_cap('list_users');
	$customer_role->add_cap('edit_users');

}

function wiaas_db_setup_create_customer_commercial_lead_table() {
	global $wpdb;

	require_once ABSPATH . 'wp-admin/includes/upgrade.php';

	$sql = "
			CREATE TABLE {$wpdb->prefix}wiaas_shop_customer_relationships (
				`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
				`customer_id` bigint(20) unsigned NOT NULL,
				`shop_owner_id` bigint(20) unsigned NOT NULL,
				`order_type` varchar(44) NOT NULL default '',
				PRIMARY KEY  (`ID`),
				KEY `relationship` (`customer_id`, `shop_owner_id`),
				KEY `order_type` (`order_type`)
			) COLLATE {$wpdb->collate};
		";

	dbDelta( $sql );
}


function wiaas_create_organization_roles_capabilities() {
	$roles = array( 'commercial_lead', 'supplier', 'customer', 'administrator');
	foreach ($roles as $role) {
		Groups_Capability::create( array( 'capability' => 'wiaas_' . $role ));
	}
}

function wiaas_disable_processing_order_email_delivery() {
    update_option( 'woocommerce_customer_processing_order_settings', array(
        'enabled'   =>  'no',
        'subject'   =>  '',
        'heading'   =>  '',
        'mail_type' => 'html'
    ) );
}


function wiaas_db_update_update_delivery_forms() {

	$forms = GFAPI::get_forms();
	foreach ($forms as $form) {
		RGFormsModel::update_form_active($form['id'], false);
	}

	$created_forms = array();

	$actions_forms_json = file_get_contents( dirname( __FILE__ ) . '/data/delivery-forms/delivery-action-forms.json' );

	$action_forms_meta     = json_decode( $actions_forms_json, true );

	foreach ($action_forms_meta as $action_form_meta) {

		$form_id = GFAPI::add_form($action_form_meta);
		$created_forms[] = GFAPI::get_form($form_id);
	}


	$sample_form_json = file_get_contents( dirname( __FILE__ ) . '/data/delivery-forms/delivery-process-sample-form.json' );

	$sample_form    = json_decode( $sample_form_json, true );

	$sample_form = $sample_form[0];

	$sample_form_id = GFAPI::add_form($sample_form);

	RGFormsModel::update_form_active($sample_form_id, false);

	$created_forms[] = GFAPI::get_form($sample_form_id);

	do_action('gform_forms_post_import', $created_forms);
}

function wiaas_db_update_add_installation_date_delivery_action_form() {

	$action_form_json = file_get_contents( dirname( __FILE__ ) . '/data/delivery-forms/delivery-action-enter-installation-date.json' );

	$action_form_meta = json_decode( $action_form_json, true )[0];

	$action_form_id = GFAPI::add_form($action_form_meta);

	do_action('gform_forms_post_import', array( GFAPI::get_form($action_form_id) ));
}

// TODO: Remove after migration has been completed
function wiaas_db_migration_fix_user_profile_addresses() {

	$users = get_users();

	foreach ($users as $user) {

		$billing_addresses = Wiaas_Customer::get_customer_billing_addresses($user->ID);

		if (! empty($billing_addresses)) {

			foreach ($billing_addresses as $index => $billing_address) {

				switch ($billing_address['id_country_selected']) {

					case 1:
						$billing_address['country_code'] = 'se';
						break;
					case 2:
						$billing_address['country_code'] = 'dk';
						break;
					case 3:
						$billing_address['country_code'] = 'fi';
						break;
				}
				unset($billing_address['id_country_selected']);
				$billing_addresses[$index] = $billing_address;
			}

			update_user_meta( $user->ID, 'billing_addresses', $billing_addresses);
		}

		$profile_addresses = Wiaas_Customer::get_customer_profile_addresses($user->ID);

		if (! empty($profile_addresses)) {

			foreach ($profile_addresses as $index => $profile_address) {

				switch ($profile_address['id_country_selected']) {

					case 1:
						$profile_address['country_code'] = 'se';
						break;
					case 2:
						$profile_address['country_code'] = 'dk';
						break;
					case 3:
						$profile_address['country_code'] = 'fi';
						break;
				}
				unset($profile_address['id_country_selected']);
				$profile_addresses[$index] = $profile_address;
			}

			update_user_meta( $user->ID, 'profile_addresses', $profile_addresses);
		}

	}
}

// TODO: Remove after migration has been completed
function wiaas_db_migration_fix_countries() {

	$available_country_terms = get_terms(array(
		'taxonomy' => 'product_country',
		'hide_empty' => false,
	));

	foreach($available_country_terms as $country_term) {

		$code = ''; $currency = ''; $vat  = '';

		switch ($country_term->name) {

			case 'Sweden':
				$code = 'se';
				$currency = 'SEK';
				$vat = 9;
				break;
			case 'Denmark':
				$code = 'dk';
				$currency = 'DKK';
				$vat = 9;
				break;
			case 'Finland':
				$code = 'fi';
				$currency = 'EUR';
				$vat = 9;
				break;
		}

		update_term_meta($country_term->term_id, '_wiaas_country_code', $code);
		update_term_meta($country_term->term_id, '_wiaas_country_currency', $currency);
		update_term_meta($country_term->term_id, '_wiaas_country_vat', $vat);
	}

}