<?php

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


/**
 * Enable restricting visibility by user roles for products
 */
function wiaas_db_update_enable_product_by_user_role() {
	update_option('wcj_product_by_user_role_enabled', 'yes');
}

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_add_delivery_process_forms() {
	$action_type_forms_files = array(
		'delivery_action_customer_acceptance_form',
		'delivery_action_schedule_meeting',
		'delivery_action_validate_questionnaire_form',
		'delivery_action_manual_form',
	);

	// Since import action will generate form with new id, we need to remember this mapping
	// so we can update process delivery steps to point to correct form ids
	$action_type_forms_ids_mappings = array();

	$process_forms_files = array(
		'delivery_process_normal_delivery_form'
	);

	$created_forms = array();

	// import forms for delivery action types
	foreach ($action_type_forms_files as $action_type_form_file) {
		$form_json = file_get_contents( dirname( __FILE__ ) . "/data/delivery-forms/" . $action_type_form_file . '.json' );

		$form_meta     = json_decode( $form_json, true );
		$form_meta = $form_meta[0];

		$form_id = GFAPI::add_form($form_meta);

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

		$action_type_forms_ids_mappings[$form_meta['id']] = $form_id;
	}

	// import forms for delivery process
	foreach ($process_forms_files as $process_form_file) {
		$form_json = file_get_contents( dirname( __FILE__ ) . "/data/delivery-forms/" . $process_form_file . '.json' );

		$form_meta     = json_decode( $form_json, true );
		$form_meta = $form_meta[0];

		// update delivery steps forms ids with correct values
		foreach ($form_meta['feeds']['gravityflow'] as $key => $process_step_meta) {
			$process_step_target_form_id = $form_meta['feeds']['gravityflow'][$key]['meta']['target_form_id'];
			$form_meta['feeds']['gravityflow'][$key]['meta']['target_form_id'] = $action_type_forms_ids_mappings[$process_step_target_form_id];
		}

		$form_id = GFAPI::add_form($form_meta);

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

	do_action('gform_forms_post_import', $created_forms);
}

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_update_enable_order_numbers() {
	update_option('wcj_order_numbers_enabled', 'yes');
	update_option('wcj_order_number_sequential_enabled', 'no');
	update_option('wcj_order_number_counter', '0');
	update_option('wcj_order_number_counter_reset_enabled', 'no');
	update_option('wcj_order_number_prefix', '1000000');
}

function wiaas_create_broker_access_group() {
	Groups_Group::create(array(
		'name' => 'Broker',
	));
}

function wiaas_db_setup_exclusive_taxonomies() {
	update_option('radio_button_for_taxonomies_options', array(
		'taxonomies' => array(
			'product_cat',
			'product_country',
			'wiaas_document_types',
		),
		'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 );
}