<?php
/**
 * Installing DB schema and demo data.
 *
 * @since 3.0.0
 */

declare(strict_types=1);
namespace FeData\FastEvents\Core;

use FeData\FastEvents\Utilities\JsonUtil;
use FeData\FastEvents\Utilities\PluginUtil;
use FeData\FastEvents\Utilities\TimeUtil;
use FeData\FastEvents\Utilities\UniqIds;
use stdClass;

/**
 * A class of utilities for dealing with dates and times.
 */
class Activation {


	/**
	 * Get the DB Schema
	 *
	 * @since 1.0
	 * @since 1.4 Added saas_fee, saas_user_id, sender_name and sender_email to events table.
	 * @since 1.9 Added log table.
	 * @since 1.9.1.1 Re-added pdf_info field.
	 * @since 2.1.0 - Added 'personalised_input' to the qrcode table.
	 *              - Changed created_at (default: CURRENT_TIMESTAMP).
	 * @since 2.2.0 - Added coupons table.
	 *              - Added 'coupons_needed' in events table
	 *              - Added 'discount' in the orders table
	 */
	public static function get_db_schema(): string {
		global $wpdb;

		return(
		"CREATE TABLE {$wpdb->prefix}fe_event_log (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			event_id BIGINT(20) UNSIGNED NOT NULL,
			order_id BIGINT(20) UNSIGNED NOT NULL,
			created_at DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
			log_type VARCHAR(32) NOT NULL DEFAULT '',
			description TEXT NOT NULL,
			PRIMARY KEY (id),
			INDEX date1 (created_at),
			INDEX date2 (event_id, created_at),
			INDEX order_idx (order_id)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};
		CREATE TABLE {$wpdb->prefix}fe_event_qrcodes (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			order_id BIGINT(20) UNSIGNED NOT NULL,
			event_id BIGINT(20) UNSIGNED NOT NULL,
			ticket_type VARCHAR(50) NOT NULL DEFAULT '',
			qrcode VARCHAR(32) NOT NULL DEFAULT '',
			personalised_input VARCHAR(512) NULL DEFAULT NULL,
			tracking_nonce VARCHAR(16) NOT NULL DEFAULT '',
			created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			amount DECIMAL(10,4) NOT NULL DEFAULT '0.0000',
			scan_date DATETIME NULL DEFAULT NULL,
			scan_location VARCHAR(32) NOT NULL DEFAULT '',
			exit_scan TINYINT(1) NOT NULL DEFAULT '0',
			PRIMARY KEY (id),
			KEY order_id (order_id),
			UNIQUE KEY qrcode (qrcode),
			INDEX event_id (event_id)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};
		CREATE TABLE {$wpdb->prefix}fe_event_entries (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			qrcode_id BIGINT(20) UNSIGNED NOT NULL,
			scancode VARCHAR(16) NOT NULL DEFAULT '',
			tracking_lap INT NOT NULL DEFAULT '0',
			scan_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			scan_location VARCHAR(32) NOT NULL DEFAULT '',
			scan_level TINYINT(1) NOT NULL DEFAULT '1',
			PRIMARY KEY (id),
			UNIQUE KEY scan_entry (qrcode_id, scancode, tracking_lap)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};
		CREATE TABLE {$wpdb->prefix}fe_event_webhooks (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			status TINYINT(1) NOT NULL DEFAULT '0',
			name VARCHAR(100) NOT NULL DEFAULT '',
			user_id BIGINT(20) UNSIGNED NOT NULL,
			url VARCHAR(200) NOT NULL DEFAULT '',
			secret CHAR(40) NOT NULL DEFAULT '',
			retry_scheme VARCHAR(50) NOT NULL DEFAULT '',
			topic VARCHAR(50) NOT NULL DEFAULT '',
			pending INT NOT NULL DEFAULT '0',
			delivered INT NOT NULL DEFAULT '0',
			retries INT NOT NULL DEFAULT '0',
			failed INT NOT NULL DEFAULT '0',
			failure_threshold INT NOT NULL DEFAULT '60',
			last_success TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
			last_retry TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
			last_failure TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
			date_created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};
		CREATE TABLE {$wpdb->prefix}fe_events (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			start_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
			end_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
			event_name VARCHAR(50) NOT NULL DEFAULT '',
			event_date TIMESTAMP NULL DEFAULT NULL,
			event_date_format VARCHAR(32) NOT NULL DEFAULT 'Y-m-d H:i',
			event_type TINYINT(1) NOT NULL DEFAULT '1',
			stock INT NOT NULL DEFAULT '0',
			sold INT NOT NULL DEFAULT '0',
			group_type TINYINT(1) NOT NULL DEFAULT '0',
			event_group VARCHAR(16) NOT NULL DEFAULT '',
			stock_link BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
			saas_fee DECIMAL(10,2) NULL DEFAULT NULL,
			saas_user_id BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
			sender_name VARCHAR(100) NOT NULL DEFAULT '',
			sender_email VARCHAR(100) NOT NULL DEFAULT '',
			email_bcc VARCHAR(100) NOT NULL DEFAULT '',
			email_subject VARCHAR(100) NOT NULL DEFAULT '',
			email_body MEDIUMTEXT NOT NULL,
			redirect VARCHAR(256) NOT NULL DEFAULT '',
			confirm_bcc VARCHAR(100) NOT NULL DEFAULT '',
			confirm_subject VARCHAR(100) NOT NULL DEFAULT '',
			confirm_body MEDIUMTEXT NOT NULL,
			confirm_redirect VARCHAR(256) NOT NULL DEFAULT '',
			emails_needed TINYINT(1) NOT NULL DEFAULT '1',
			tickets_needed TINYINT(1) NOT NULL DEFAULT '1',
			invoice_needed TINYINT(1) NOT NULL DEFAULT '1',
			unique_users_needed TINYINT(1) NOT NULL DEFAULT '0',
			user_groups_needed TINYINT(1) NOT NULL DEFAULT '0',
			recaptcha_needed TINYINT(1) NOT NULL DEFAULT '0',
			confirmation_emails_needed TINYINT(1) NOT NULL DEFAULT '0',
			confirmation_timeout INT NOT NULL DEFAULT '0',
			add_dashboard_orders_needed TINYINT(1) NOT NULL DEFAULT '1',
			test_payments_needed TINYINT(1) NOT NULL DEFAULT '1',
			seats_needed TINYINT(1) NOT NULL DEFAULT '0',
			webhooks_needed TINYINT(1) NOT NULL DEFAULT '0',
			reload_on_exit_needed TINYINT(1) NOT NULL DEFAULT '0',
			tracking_needed TINYINT(1) NOT NULL DEFAULT '0',
			coupons_needed TINYINT(1) NOT NULL DEFAULT '0',
			terms VARCHAR(256) DEFAULT NULL,
			pdf_info TEXT NOT NULL,
			input_fields TEXT NOT NULL,
			scan_keys TEXT NOT NULL,
			tracking_info MEDIUMTEXT NOT NULL,
			PRIMARY KEY (id)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};
		CREATE TABLE {$wpdb->prefix}fe_event_orders (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			uid CHAR(40) NOT NULL DEFAULT '',
			event_id BIGINT(20) UNSIGNED NOT NULL,
			event_date DATETIME NULL DEFAULT NULL,
			payment_id VARCHAR(255) NOT NULL DEFAULT '',
			payment_status VARCHAR(32) NOT NULL DEFAULT '',
			custom_status VARCHAR(32) NOT NULL DEFAULT '',
			created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			name VARCHAR(50) NOT NULL DEFAULT '',
			email VARCHAR(100) NOT NULL DEFAULT '',
			input_fields TEXT NOT NULL,
			num_tickets INT NOT NULL DEFAULT '0',
			total DECIMAL(10,4) NOT NULL DEFAULT '0.0000',
			discount DECIMAL(10,4) NOT NULL DEFAULT '0.0000',
			ip_address VARCHAR(45) NOT NULL,
			PRIMARY KEY (id),
			UNIQUE KEY uid (uid(40)),
			INDEX event_id (event_id),
			INDEX email (email)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};
		CREATE TABLE {$wpdb->prefix}fe_event_user_groups (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			event_id BIGINT(20) UNSIGNED NOT NULL,
			name VARCHAR(50) NOT NULL DEFAULT '',
			email VARCHAR(100) NOT NULL DEFAULT '',
			num_tickets INT NOT NULL DEFAULT '0',	
			PRIMARY KEY (id),
			UNIQUE KEY email (event_id,email)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};
		CREATE TABLE {$wpdb->prefix}fe_event_seats (
			id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
			event_id BIGINT(20) UNSIGNED NOT NULL,
			num_seats INT UNSIGNED NOT NULL DEFAULT '0',
			last_seat_id INT UNSIGNED NOT NULL DEFAULT '0',
			open_seats TINYINT(1) NOT NULL DEFAULT '0',
			seat_format VARCHAR(32) NOT NULL DEFAULT '',
			seat_config MEDIUMTEXT NOT NULL,
			seat_map MEDIUMTEXT NOT NULL,
			linked_event BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
			PRIMARY KEY (id),
			UNIQUE INDEX event_id (event_id)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};
		CREATE TABLE {$wpdb->prefix}fe_event_coupons ( 
			id BIGINT NOT NULL AUTO_INCREMENT, 
			code VARCHAR(32) NOT NULL, 
			description VARCHAR(100) NOT NULL, 
			date_created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, 
			date_modified DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			type ENUM('percent','fixed') NOT NULL DEFAULT 'fixed',
			amount DECIMAL(10,2) NOT NULL, 
			usage_limit INT UNSIGNED NOT NULL DEFAULT '1', 
			usage_limit_per_user INT UNSIGNED NOT NULL DEFAULT '1', 
			used INT UNSIGNED NOT NULL DEFAULT '0',
			last_used DATETIME NULL,
			start_date DATETIME NOT NULL, 
			end_date DATETIME NOT NULL, 
			events VARCHAR(64) NULL, 
			tickets VARCHAR(256) NULL, 
			minimum_tickets INT UNSIGNED NULL, 
			maximum_tickets INT UNSIGNED NULL, 
			minimum_amount DECIMAL(10,2) NULL, 
			maximum_amount DECIMAL(10,2) NULL,
			email VARCHAR(100) NULL,
			PRIMARY KEY (id), 
			UNIQUE INDEX code_index (code)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};
		CREATE TABLE {$wpdb->prefix}fe_event_email_log (
			id BIGINT NOT NULL AUTO_INCREMENT,
			event_id BIGINT(20) UNSIGNED NOT NULL,
			order_id BIGINT(20) UNSIGNED NOT NULL,
			date_created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			date_modified DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
			provider_id ENUM('local','smtp','amazon','brevo','mailgun','mailjet','postmark','sendgrid','smtp2go','sparkpost') NOT NULL DEFAULT 'local',
			email_type ENUM('normal','confirm','free') NOT NULL DEFAULT 'normal',
			provider_email_id VARCHAR(100) NOT NULL,
			sender_email VARCHAR(100) NOT NULL,
			receiver_email VARCHAR(100) NOT NULL,
			email_subject VARCHAR(100) NOT NULL,
			email_events TEXT NOT NULL,
			PRIMARY KEY (id),
			UNIQUE KEY provider_email_id_index (provider_email_id),
			UNIQUE KEY order_date_index (order_id, date_created)
		) ENGINE=InnoDB {$wpdb->get_charset_collate()};"
		);
	}


	/**
	 * Install the demo data
	 *
	 * @since 1.0
	 * @since 1.4 - Added new SaaS settings
	 *            - Added 'no_border' for PDF tickets
	 *            - Added cache time for Order forms
	 * @since 1.9.1 Added email_webhook flag.
	 * @since 2.0.1 Removed 'pertype' in the event PDF structure.
	 * @since 2.0.3 Check if demodata is already installed.
	 * @since 2.2.0 Added discount information.
	 * @since 2.3.0 Use fast_events_json_encode.
	 * @since 2.4.0 Added SMTP2GO in options and "remain" field for tickets.
	 * @since 2.5.0 Added email log entry for event id 2.
	 * @since 3.0.0 Return the list of inserted post-IDs.
	 *
	 * @return array
	 */
	public static function install_demo_data(): array {
		global $wpdb;

		$urlparts = wp_parse_url( home_url() );
		$domain   = preg_replace( '/^www\./', '', $urlparts['host'] );
		$post_ids = array();

		// Insert the eticket template for OpenAir.
		$wp_upload_dir = wp_upload_dir();
		$filename      = PluginUtil::plugin_path() . '/assets/demo-data/OpenAir-Eticket.pdf';
		$upload_file   = $wp_upload_dir['path'] . '/' . basename( $filename );
		$filetype      = wp_check_filetype( basename( $filename ) );
		copy( $filename, $upload_file );
		$eticket_id = wp_insert_attachment(
			array(
				'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
				'post_mime_type' => $filetype['type'],
				'post_title'     => __( 'Eticket template OpenAir', 'fast-events' ),
				'post_content'   => '',
				'post_status'    => 'inherit',
			),
			$upload_file
		);
		if ( ! is_wp_error( $eticket_id ) ) {
			$post_ids[]  = $eticket_id;
			$attach_data = wp_generate_attachment_metadata( $eticket_id, $upload_file );
			wp_update_attachment_metadata( $eticket_id, $attach_data );
			add_post_meta( $eticket_id, 'fast_events_template_type', 'ticket', true );
		}

		// Insert the eticket template for Forest 2 forest tour.
		$filename    = PluginUtil::plugin_path() . '/assets/demo-data/F2ftour.pdf';
		$upload_file = $wp_upload_dir['path'] . '/' . basename( $filename );
		$filetype    = wp_check_filetype( basename( $filename ) );
		copy( $filename, $upload_file );
		$f2f_id = wp_insert_attachment(
			array(
				'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
				'post_mime_type' => $filetype['type'],
				'post_title'     => __( 'Eticket template Forest to Forest', 'fast-events' ),
				'post_content'   => '',
				'post_status'    => 'inherit',
			),
			$upload_file
		);
		if ( ! is_wp_error( $f2f_id ) ) {
			$post_ids[]  = $f2f_id;
			$attach_data = wp_generate_attachment_metadata( $f2f_id, $upload_file );
			wp_update_attachment_metadata( $f2f_id, $attach_data );
			add_post_meta( $f2f_id, 'fast_events_template_type', 'ticket', true );
		}

		// Insert the invoice template.
		$filename    = PluginUtil::plugin_path() . '/assets/demo-data/OpenAir-Invoice.pdf';
		$upload_file = $wp_upload_dir['path'] . '/' . basename( $filename );
		copy( $filename, $upload_file );
		$invoice_id = wp_insert_attachment(
			array(
				'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
				'post_mime_type' => $filetype['type'],
				'post_title'     => __( 'Invoice template Openair', 'fast-events' ),
				'post_content'   => '',
				'post_status'    => 'inherit',
			),
			$upload_file
		);
		if ( ! is_wp_error( $invoice_id ) ) {
			$post_ids[]  = $invoice_id;
			$attach_data = wp_generate_attachment_metadata( $invoice_id, $upload_file );
			wp_update_attachment_metadata( $invoice_id, $attach_data );
			add_post_meta( $invoice_id, 'fast_events_template_type', 'invoice', true );
		}

		// Set defaults for tracking info.
		$tracking_info                     = new stdClass();
		$tracking_info->start_tracking     = wp_date( 'Y-m-d' ) . ' 00:00:00';
		$tracking_info->stop_tracking      = wp_date( 'Y-m-d', TimeUtil::local_time() + ( 24 * 3600 * 14 ) ) . ' 14:00:00';
		$tracking_info->geofence_radius    = 200;
		$tracking_info->distance_filter    = 10;
		$tracking_info->no_entry_scan      = false;
		$tracking_info->force_tracking_app = false;
		$tracking_info->tr_help_url        = home_url();
		$tracking_info->tr_help_tel        = '1234567890';
		$tracking_info->tr_help_txt        = __( 'Emergency information', 'fast-events' );
		$tracking_info->tracks             = array();

		// Insert the "Error" page.
		$post_id = wp_insert_post(
			array(
				'post_title'   => __( 'Error', 'fast-events' ),
				'post_content' => '<!-- wp:paragraph -->' . PHP_EOL .
									'<p>' . __( 'Payment has not been confirmed. This may be because you cancelled the payment, or because we will be notified at a later date that the payment has been processed. If you have paid, you can use the link below to check again that your payment has been confirmed by us.', 'fast-events' ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->' . PHP_EOL . PHP_EOL . '<!-- wp:shortcode -->' . PHP_EOL .
									'[fe_retry][/fe_retry]' . PHP_EOL .
									'<!-- /wp:shortcode -->' . PHP_EOL . PHP_EOL . '<!-- wp:paragraph -->' . PHP_EOL .
									/* translators: %s is the domain */
									'<p>' . sprintf( __( 'If you have not received an email with your tickets within 3 hours, please email %1$s with your order details and we will assist you as soon as possible.', 'fast-events' ), 'info@' . $domain ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->',
				'post_status'  => 'publish',
				'post_type'    => 'page',
				'post_name'    => 'order-error',
			)
		);
		if ( ! is_wp_error( $post_id ) ) {
			$post_ids[] = $post_id;
		}

		// Insert the "Thank you" page for an OpenAir order.
		$post_id = wp_insert_post(
			array(
				'post_title'   => __( 'Thank you for your Vinyl Open Air order', 'fast-events' ),
				'post_content' => '<!-- wp:paragraph -->' . PHP_EOL .
									/* translators: %s is the year */
									'<p>' . sprintf( __( 'It is great that you are coming to the Vinyl Open Air %1$s festival in DJ-Town.', 'fast-events' ), wp_date( 'Y' ) ) . ' ' .
									__( 'We hope you enjoy watching and listening to our DJs. All the DJs are looking forward to it and will make it a spectacular show.', 'fast-events' ) . PHP_EOL .
									__( 'Below you can find all tickets that you must first personalise before you can download them.', 'fast-events' ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->' . PHP_EOL . PHP_EOL . '<!-- wp:shortcode -->' . PHP_EOL .
									'[fe_personalise cdn]' . PHP_EOL .
									'<!-- /wp:shortcode -->' . PHP_EOL . PHP_EOL . '<!-- wp:paragraph -->' . PHP_EOL .
									'<p>' . __( 'Kind regards,', 'fast-events' ) .
									'<br>' . __( 'The DJ Festival team', 'fast-events' ) . '</p>' .
									'<!-- /wp:paragraph -->' . PHP_EOL . PHP_EOL . '<!-- wp:paragraph -->' . PHP_EOL .
									'<p>---<br>' . __( 'Shortly after ordering, you will receive an email with more information and how to download your e-tickets.', 'fast-events' ) . ' ' .
									__( 'If you do not receive the email, this is obviously annoying. What can you do?', 'fast-events' ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->' . PHP_EOL . PHP_EOL . '<!-- wp:list -->' . PHP_EOL .
									'<ul><!-- wp:list-item -->' . PHP_EOL .
									'<li>' . __( 'Is the email in the spam folder?', 'fast-events' ) . '</li>' . PHP_EOL .
									'<!-- /wp:list-item -->' . PHP_EOL . PHP_EOL . '<!-- wp:list-item -->' . PHP_EOL .
									'<li>' . __( 'Check the virus scanner or any other anti-spam tools that are on your computer.', 'fast-events' ) . '</li>' . PHP_EOL .
									'<!-- /wp:list-item -->' . PHP_EOL . PHP_EOL . '<!-- wp:list-item -->' . PHP_EOL .
									'<li>' . __( 'Some email clients have an "unread email" folder. Is the email there?', 'fast-events' ) . '</li>' . PHP_EOL .
									'<!-- /wp:list-item -->' . PHP_EOL . PHP_EOL . '<!-- wp:list-item -->' . PHP_EOL . '<li>' .
									__( 'Do you read your email via Outlook, Thunderbird, or another tool?', 'fast-events' ) . ' ' .
									__( 'Check your provider\'s webmail to see if the email is visible there.', 'fast-events' ) . ' ' .
									__( 'For Gmail accounts, visit https://gmail.com and for Microsoft accounts, visit https://outlook.com.', 'fast-events' ) . '</li>' . PHP_EOL .
									'<!-- /wp:list-item -->' . PHP_EOL . PHP_EOL . '<!-- wp:list-item -->' . PHP_EOL .
									'<li>' . __( 'Send an email to yourself from another account and see if it arrives.', 'fast-events' ) . '</li>' . PHP_EOL .
									'<!-- /wp:list-item --></ul>' . PHP_EOL .
									'<!-- /wp:list -->' . PHP_EOL . PHP_EOL .
									'<!-- wp:paragraph -->' . PHP_EOL .
									/* translators: %s is the domain */
									'<p>' . sprintf( __( 'If this does not solve the problem, please send an email to %1$s with the order number (which can be found in your bank\'s payment history) and your order details.', 'fast-events' ), 'info@' . $domain ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->',
				'post_status'  => 'publish',
				'post_type'    => 'page',
				'post_name'    => 'order-thankyou',
			)
		);
		if ( ! is_wp_error( $post_id ) ) {
			$post_ids[] = $post_id;
		}

		// Insert the Terms and conditions.
		$post_id = wp_insert_post(
			array(
				'post_title'   => __( 'Terms and conditions', 'fast-events' ),
				/* translators: %s is the year */
				'post_content' => '<!-- wp:paragraph -->' . PHP_EOL .
									'<p>' . __( 'Put your general terms and conditions here.', 'fast-events' ) . '<br>1) ...<br>2) ...<br>...</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->',
				'post_status'  => 'publish',
				'post_type'    => 'page',
				'post_name'    => 'terms',
			)
		);
		if ( ! is_wp_error( $post_id ) ) {
			$post_ids[] = $post_id;
		}

		// Insert the Order page for OpenAir.
		$post_id = wp_insert_post(
			array(
				'post_title'   => __( 'Order Vinyl Open Air tickets', 'fast-events' ),
				/* translators: %s is the year */
				'post_content' => '<!-- wp:paragraph -->' . PHP_EOL .
									/* translators: %s is the year */
									'<p>' . sprintf( __( 'Order your tickets for Vinyl Open Air %1$s here.', 'fast-events' ), wp_date( 'Y' ) ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->' . PHP_EOL . PHP_EOL .
									'<!-- wp:shortcode -->' . PHP_EOL . '[fast_events id=2]' . PHP_EOL . '<!-- /wp:shortcode -->' . PHP_EOL . PHP_EOL .
									'<!-- wp:paragraph -->' . PHP_EOL .
									'<p>' . __( 'Once payment has been made, an email will be sent to the address provided with further instructions on how to download the tickets.', 'fast-events' ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->',
				'post_status'  => 'publish',
				'post_type'    => 'page',
				'post_name'    => 'order-etickets',
			)
		);
		if ( ! is_wp_error( $post_id ) ) {
			$post_ids[] = $post_id;
		}

		// Insert the "Thank you" page for a Forest to forest order.
		$post_id = wp_insert_post(
			array(
				'post_title'   => __( 'Thank you for your Forest to Forest tour order', 'fast-events' ),
				'post_content' => '<!-- wp:paragraph -->' . PHP_EOL .
									'<p>' . __( 'Thank you for joining us on the Forest to Forest tour.', 'fast-events' ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->' . PHP_EOL . PHP_EOL . '<!-- wp:paragraph -->' . PHP_EOL .
									'<p>' . __( 'By clicking on the link below, you can download the ticket.', 'fast-events' ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->' . PHP_EOL . PHP_EOL . '<!-- wp:shortcode -->' . PHP_EOL .
									'[fe_download downloadtext="' . __( 'Download tickets', 'fast-events' ) . '"]' . PHP_EOL .
									'<!-- /wp:shortcode -->' . PHP_EOL . PHP_EOL . '<!-- wp:paragraph -->' . PHP_EOL .
									'<p>' . __( 'Kind regards,', 'fast-events' ) . '<br>' .
									__( 'The Forest to Forest team', 'fast-events' ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->',
				'post_status'  => 'publish',
				'post_type'    => 'page',
				'post_name'    => 'f2f-thankyou',
			)
		);
		if ( ! is_wp_error( $post_id ) ) {
			$post_ids[] = $post_id;
		}

		// Insert the Order page for Forest to forest tour.
		$post_id = wp_insert_post(
			array(
				'post_title'   => __( 'Order Forest to Forest tickets', 'fast-events' ),
				'post_content' => '<!-- wp:paragraph -->' . PHP_EOL .
									'<p>' . __( 'You can order your tickets for the Forest to Forest tour here.', 'fast-events' ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->' . PHP_EOL . PHP_EOL .
									'<!-- wp:shortcode -->' . PHP_EOL . '[fast_events id=3]' . PHP_EOL . '<!-- /wp:shortcode -->' . PHP_EOL . PHP_EOL .
									'<!-- wp:paragraph -->' . PHP_EOL .
									'<p>' . __( 'After payment, a link to download the ticket will be displayed.', 'fast-events' ) . '</p>' . PHP_EOL .
									'<!-- /wp:paragraph -->',
				'post_status'  => 'publish',
				'post_type'    => 'page',
				'post_name'    => 'f2f-etickets',
			)
		);
		if ( ! is_wp_error( $post_id ) ) {
			$post_ids[] = $post_id;
		}

		// Set the DJ Festival event (=2).
		$wpdb->insert(
			$wpdb->prefix . 'fe_events',
			array(
				'id'                         => 2,
				'event_name'                 => 'Vinyl Open Air ' . wp_date( 'Y' ),
				'event_type'                 => 1,
				'event_date'                 => wp_date( 'Y-m-d', TimeUtil::local_time() + ( 24 * 3600 * 14 ) ) . ' 14:00:00',
				'event_date_format'          => 'Y-m-d H:i',
				'start_date'                 => wp_date( 'Y-m-d' ) . ' 00:00:00',
				'end_date'                   => wp_date( 'Y-m-d', TimeUtil::local_time() + ( 24 * 3600 * 14 ) ) . ' 14:00:00',
				'stock'                      => 10000,
				'sold'                       => 4,
				/* translators: %s is the year */
				'email_subject'              => sprintf( __( 'Your Vinyl Open Air %1$s tickets', 'fast-events' ), wp_date( 'Y' ) ),
				'email_body'                 => '<div style="width:100%;max-width:600px;">' .
												'<div style="font-size:150%;font-weight:bold;margin-bottom:30px;">Vinyl Open Air ' . wp_date( 'Y' ) .
												'</div>' . PHP_EOL . PHP_EOL . PHP_EOL .
												'<div style="margin-bottom:20px;">' . __( 'Dear', 'fast-events' ) . ' {%NAME%},</div>' . PHP_EOL . PHP_EOL .
												'<div style="margin-bottom:10px;">' . __( 'Thank you for placing your order. You can personalise your tickets by clicking the button below.', 'fast-events' ) . '</div>' . PHP_EOL . PHP_EOL .
												'<div style="margin-bottom:20px;">' . __( 'After personalization you can download all tickets.', 'fast-events' ) . '</div>' . PHP_EOL . PHP_EOL .
												'<div style="margin-bottom:20px;">{%PERSONALISE%}</div>' . PHP_EOL . PHP_EOL .
												'<div style="margin-bottom:15px;">' . __( 'For more information, please visit: ', 'fast-events' ) . '<a href="https://vinyl-openair.nl" target="_blank" rel="noreferrer noopener">https://vinyl-openair.nl</a></div>' . PHP_EOL . PHP_EOL .
												'<div style="margin-bottom:25px;">' . __( 'Kind regards,', 'fast-events' ) . '</div>' . PHP_EOL . PHP_EOL . PHP_EOL .
												'<div>' . __( 'The DJ Festival team', 'fast-events' ) . '</div></div>',
				'unique_users_needed'        => 0,
				'user_groups_needed'         => 0,
				'recaptcha_needed'           => 0,
				'confirmation_emails_needed' => 0,
				'seats_needed'               => 0,
				'webhooks_needed'            => 0,
				'reload_on_exit_needed'      => 0,
				'tracking_needed'            => 0,
				'coupons_needed'             => 1,
				'tracking_info'              => JsonUtil::fast_events_json_encode( $tracking_info ),
				'redirect'                   => home_url() . '/order-thankyou/',
				'terms'                      => __( 'I agree to ', 'fast-events' ) . ' <a href="' . home_url() . '/terms" target="_blank" rel="noreferrer noopener">' . __( 'Terms and Conditions', 'fast-events' ) . '</a>',
				'pdf_info'                   => '{"ticket":{"id":' . $eticket_id .
												',"x":40,"y":150,"rotate":0,"scale":100,"no_border":0,"types":{"Silver":' . $eticket_id . ',"Gold (Backstage)":' . $eticket_id . '}},"invoice":{"id":' . $invoice_id .
												',"vat":1,"name":{"x":27,"y":60},"invoiceno":{"x":127,"y":65},"line":{"x":25,"y":92,"w":158}}}',
				'input_fields'               => '{"fields":[{"id":"v36c12","name":"Attendee","type":"text","value":"","min":"","max":"","required":1,"personalise":1},{"id":"v6a7f8","name":"Preference","type":"select","value":"Rock,Jazz,Classic,Pop","min":"","max":"","required":1,"personalise":1},{"id":"v71ba9","name":"Beverage","type":"select","value":"Beer,Wine,Whiskey","min":"","max":"","required":1,"personalise":1},{"id":"v59f29","name":"Entrance","type":"select","value":"North,West,East,South","min":"","max":"","required":1,"personalise":0}],"tickets":[{"name":"Silver","id":"v023b9","stock":"","sold":2,"price":"25.00","volume_price":[{"4":20},{"10":18.75},{"15":18.25}],"min":0,"max":50,"vat":"21","cnt":1,"remain":0,"personalise":"Attendee,Preference","layout":"Order-id,Name,Email"},{"name":"Gold (Backstage)","id":"v1a311","stock":100,"sold":2,"price":"40.00","min":0,"max":50,"vat":"21","cnt":1,"remain":0,"personalise":"Attendee,Preference,Beverage","layout":"Entrance,Attendee,Preference,Beverage,Order-id"}],"submit":{"name":"' . __( 'Pay', 'fast-events' ) . '"},"unique_users":{"master_eventids":""},"user_group":{"id":0,"eventid":"","roles":"","rest_url":"","rest_header":""}}',
				'scan_keys'                  => '{"' . UniqIds::uniq_id() . '":{"tickets":"","entry":0,"location":"' . __( 'Main entrance', 'fast-events' ) . '","date_format":"l, j F H:i:s"},"' . UniqIds::uniq_id() . '":{"tickets":"Gold (Backstage)","entry":1,"location":"' . __( 'Backstage entrance', 'fast-events' ) . '","date_format":"l, j F H:i:s"}}',
			)
		);

		// Insert seats row; not really needed, just for integrity.
		$wpdb->insert( $wpdb->prefix . 'fe_event_seats', array( 'event_id' => 2 ) );

		// Insert coupon for both events.
		$wpdb->insert(
			$wpdb->prefix . 'fe_event_coupons',
			array(
				'id'                   => 1,
				'code'                 => 'CP01',
				'description'          => __( 'Discount code for a 10% discount on all tickets', 'fast-events' ),
				'type'                 => 'percent',
				'amount'               => 10.0000,
				'usage_limit'          => 1000,
				'usage_limit_per_user' => 10,
				'start_date'           => wp_date( 'Y-m-d' ) . ' 00:00:00',
				'end_date'             => wp_date( 'Y-m-d', TimeUtil::local_time() + ( 24 * 3600 * 30 ) ) . ' 14:00:00',
			)
		);

		// Insert the order.
		$wpdb->insert(
			$wpdb->prefix . 'fe_event_orders',
			array(
				'id'             => 1,
				'uid'            => UniqIds::uniq_order_id(),
				'event_id'       => 2,
				'event_date'     => wp_date( 'Y-m-d', TimeUtil::local_time() + ( 24 * 3600 * 14 ) ) . ' 14:00:00',
				'payment_status' => 'dashboard',
				'created_at'     => wp_date( 'Y-m-d H:i:s' ),
				'name'           => 'John Doe',
				'email'          => 'JohnDoe@JohnDoe12.com',
				'input_fields'   => '{"fields":[{"name":"Entrance","value":"West"}],"tickets":[{"name":"Silver","price":"25","vat":"21","cflag":1,"count":2},{"name":"Gold (Backstage)","price":"40","vat":"21","cflag":1,"count":2}],"order_amount":"130.00","order_vat":"22.56","events":[1],"orders":[1],"invoice":{}}',
				'num_tickets'    => 4,
				'total'          => 130.0000,
				'discount'       => 0.0000,
				'ip_address'     => '1.2.3.4',
			)
		);

		// Insert qrcode 'Silver 1'.
		$wpdb->insert(
			$wpdb->prefix . 'fe_event_qrcodes',
			array(
				'id'                 => 1,
				'order_id'           => 1,
				'event_id'           => 2,
				'ticket_type'        => 'Silver',
				'qrcode'             => UniqIds::uniq_id(),
				'created_at'         => wp_date( 'Y-m-d H:i:s' ),
				'personalised_input' => '{"personalised":true,"shared":true,"locked":true,"data":{"Attendee":"John Doe","Preference":"Jazz"}}',
				'amount'             => 25.0000,
			)
		);

		// Insert qrcode 'Silver 2'.
		$wpdb->insert(
			$wpdb->prefix . 'fe_event_qrcodes',
			array(
				'id'                 => 2,
				'order_id'           => 1,
				'event_id'           => 2,
				'ticket_type'        => 'Silver',
				'qrcode'             => UniqIds::uniq_id(),
				'created_at'         => wp_date( 'Y-m-d H:i:s' ),
				'personalised_input' => '{"personalised":true,"shared":true,"locked":true,"data":{"Attendee":"Sara Doe","Preference":"Rock"}}',
				'amount'             => 25.0000,
			)
		);

		// Insert qrcode 'Gold (Backstage) 1'.
		$wpdb->insert(
			$wpdb->prefix . 'fe_event_qrcodes',
			array(
				'id'                 => 3,
				'order_id'           => 1,
				'event_id'           => 2,
				'ticket_type'        => 'Gold (Backstage)',
				'qrcode'             => UniqIds::uniq_id(),
				'created_at'         => wp_date( 'Y-m-d H:i:s' ),
				'personalised_input' => '{"personalised":true,"shared":true,"locked":true,"data":{"Attendee":"Gilbert Doe","Preference":"Rock","Beverage":"Wine"}}',
				'amount'             => 40.0000,
			)
		);

		// Insert qrcode 'Gold (Backstage) 2'.
		$wpdb->insert(
			$wpdb->prefix . 'fe_event_qrcodes',
			array(
				'id'                 => 4,
				'order_id'           => 1,
				'event_id'           => 2,
				'ticket_type'        => 'Gold (Backstage)',
				'qrcode'             => UniqIds::uniq_id(),
				'created_at'         => wp_date( 'Y-m-d H:i:s' ),
				'personalised_input' => '{"personalised":true,"shared":true,"locked":true,"data":{"Attendee":"Mary Doe","Preference":"Jazz","Beverage":"Beer"}}',
				'amount'             => 40.0000,
			)
		);

		// Insert the email log.
		$wpdb->insert(
			$wpdb->prefix . 'fe_event_email_log',
			array(
				'id'                => 1,
				'event_id'          => 2,
				'order_id'          => 1,
				'date_created'      => wp_date( 'Y-m-d H:i:s' ),
				'date_modified'     => wp_date( 'Y-m-d H:i:s' ),
				'provider_id'       => 'local',
				'email_type'        => 'normal',
				'provider_email_id' => UniqIds::uniq_order_id(),
				'sender_email'      => 'info@' . $domain,
				'receiver_email'    => 'JohnDoe@JohnDoe12.com',
				/* translators: %s is the year */
				'email_subject'     => sprintf( __( 'Your Vinyl Open Air %1$s tickets', 'fast-events' ), wp_date( 'Y' ) ),
				'email_events'      => wp_json_encode( array() ),
			)
		);

		// Set the Forest to forest tour event (=3).
		$tracking_info->tr_help_txt =
			'<p>' . __( 'We want everyone to enjoy our mountain bike tour.', 'fast-events' ) . ' ' .
			__( 'Please make sure that you are well-prepared. Nevertheless, it is possible that something happens to you, and we would like to help you as best we can. Below are a few steps that you can use to decide what to do: ', 'fast-events' ) .
			'<br></p><p><b>' . __( '1) If your life is in danger, call 112 immediately.', 'fast-events' ) . '</b>' .
			'</p><p><b>2) </b>' .
			__( 'If your injury is such that you can continue cycling on your own or with the help of others, cycle to the next emergency station.', 'fast-events' ) . ' ' .
			__( 'You can see exactly where you are on the route with your phone, so you know how far it is to the first aid post.', 'fast-events' ) . ' ' .
			__( 'In this case, do not call the emergency number.', 'fast-events' ) . '</p><p><b>3) </b>' .
			__( 'If the situation is more serious, call the emergency number above.', 'fast-events' ) . ' ' .
			__( 'It is also possible to use this number via WhatsApp, which is very convenient in order to give your exact location.', 'fast-events' ) . ' ' .
			__( 'If you really can\'t go any further, it may be decided that a first-aider on a quad bike should come to the scene to assess the situation and decide what to do next.', 'fast-events' ) .
			'</p>';
		$tracking_info->tracks      =
			json_decode(
				'[{"id":224853359,"name":"Forest route","points":[{"id":279255920,"name":"' .
				__( 'Checkpoint', 'fast-events' ) . ' 1","symbol":"1899","color":"#0288d1",' .
				'"description":"' . __( 'Checkpoint is located near two large oak trees.', 'fast-events' ) .
				'","is_checkpoint":false,"coordinates":[5.8604167,52.3495652]},' .
				'{"id":163539334,"name":"' . __( 'Checkpoint', 'fast-events' ) .
				' 2","symbol":"1899","color":"#0288d1","description":"' . __( 'Checkpoint before the red and green traffic lights.', 'fast-events' ) .
				'","is_checkpoint":false,"coordinates":[5.84875,52.3620937]},{"id":413720957,"name":"' .
				__( 'Checkpoint', 'fast-events' ) . ' 3",' .
				'"symbol":"1899","color":"#0288d1","description":"' .
				__( 'The checkpoint is between the first aid post and McDonalds.', 'fast-events' ) . '","is_checkpoint"' .
				':false,"coordinates":[5.8460684,52.3552542]},{"id":275626084,"name":"' .
				__( 'Joe\'s Juices', 'fast-events' ) . '","symbol":"1577","color":"#7cb342",' .
				'"description":"' . __( 'Fruit drinks from 11.00 - 16.00', 'fast-events' ) .
				'","is_checkpoint":false,"coordinates":[5.8548928,52.3556769]},{"id":591546256,' .
				'"name":"' . __( 'Water', 'fast-events' ) . '","symbol":"1577","color":"#7cb342","description":"' .
				__( 'Bottles of water', 'fast-events' ) . '","is_checkpoint":false,"coordinates":' .
				'[5.8483482,52.3532392]},{"id":430067048,"name":"' . __( 'Halfway', 'fast-events' ) .
				'","symbol":"1624","color":"#e65100","description":"' . __( 'First Aid station', 'fast-events' ) .
				'","is_checkpoint":false,"coordinates":[5.8454085,52.360768]},{"id":548323320,"name":"' .
				__( 'Start - finish', 'fast-events' ) . '","symbol":"1624",' .
				'"color":"#e65100","description":"' . __( 'First Aid', 'fast-events' ) .
				'","is_checkpoint":false,"coordinates":[5.8602575,52.3425012]}],' .
				'"line":{"id":38680418,"name":"' . __( 'Forest route', 'fast-events' ) .
				'","color":"#000000","coordinates":[[5.8603722,52.3423688],[5.8659082,' .
				'52.3437584],[5.8603292,52.3496308],[5.84875,52.3620937],[5.8430208,52.3598464],[5.8415187,52.3593812],[5.855451,' .
				'52.3468926],[5.8602575,52.3425012]]},"legend":{"1899":"' . __( 'Checkpoints', 'fast-events' ) .
				'","1577":"' . __( 'Eateries', 'fast-events' ) .
				'","1624":"' . __( 'First Aid', 'fast-events' ) . '"}}]'
			);
		$wpdb->insert(
			$wpdb->prefix . 'fe_events',
			array(
				'id'                         => 3,
				'event_name'                 => 'Forest to forest tour',
				'event_type'                 => 1,
				'event_date'                 => wp_date( 'Y-m-d', TimeUtil::local_time() + ( 24 * 3600 * 14 ) ) . ' 10:00:00',
				'event_date_format'          => 'Y-m-d H:i',
				'start_date'                 => wp_date( 'Y-m-d' ) . ' 00:00:00',
				'end_date'                   => wp_date( 'Y-m-d', TimeUtil::local_time() + ( 24 * 3600 * 14 ) ) . ' 10:00:00',
				'stock'                      => 750,
				'sold'                       => 1,
				'email_subject'              => '',
				'email_body'                 => '',
				'emails_needed'              => 0,
				'invoice_needed'             => 0,
				'unique_users_needed'        => 0,
				'user_groups_needed'         => 0,
				'recaptcha_needed'           => 0,
				'confirmation_emails_needed' => 0,
				'seats_needed'               => 0,
				'webhooks_needed'            => 0,
				'reload_on_exit_needed'      => 0,
				'tracking_needed'            => 1,
				'coupons_needed'             => 0,
				'tracking_info'              => JsonUtil::fast_events_json_encode( $tracking_info ),
				'redirect'                   => home_url() . '/f2f-thankyou/',
				'terms'                      => __( 'I agree to ', 'fast-events' ) . '<a href="' . home_url() . '/terms" target="_blank" rel="noreferrer noopener">' . __( 'Terms and Conditions', 'fast-events' ) . '</a>',
				'pdf_info'                   => '{"ticket":{"id":' . $f2f_id .
												',"x":40,"y":150,"rotate":0,"scale":100,"no_border":0,"types":{"Junior":' . $f2f_id . ',"Senior":' . $f2f_id . '}},"invoice":{"id":""' .
												',"vat":1,"name":{"x":27,"y":60},"invoiceno":{"x":127,"y":65},"line":{"x":25,"y":92,"w":158}}}',
				'input_fields'               => '{"fields":[],"tickets":[{"name":"Junior","id":"v023b8","stock":100,"sold":1,"price":"25.00","min":0,"max":1,"vat":"21","cnt":1,"remain":0,"personalise":"","layout":"Order-id,Name,Email"},' .
												'{"name":"Senior","id":"v1a312","stock":"","sold":0,"price":"30.00","min":0,"max":1,"vat":"21","cnt":1,"remain":0,"personalise":"","layout":"Order-id,Name,Email"}],"submit":{"name":"' . __( 'Pay', 'fast-events' ) . '"},"unique_users":{"master_eventids":""},"user_group":{"id":0,"eventid":"","roles":"","rest_url":"","rest_header":""}}',
				'scan_keys'                  => '{"F3rdp7H9MaBoFonQ":{"tickets":"","entry":0,"location":"' . __( 'Start', 'fast-events' ) . '","date_format":"l, j F H:i"},"gHNduTedoHqXgBHJ":{"tickets":"","entry":1,"location":"' . __( 'Checkpoint', 'fast-events' ) . ' 1",' .
												'"date_format":"l, j F H:i"},"2MDzeidKn2AJtp24":{"tickets":"","entry":1,"location":"' . __( 'Checkpoint', 'fast-events' ) . ' 2","date_format":"l, j F H:i"},"VUtLaxncZtYxUV2J":' .
												'{"tickets":"","entry":1,"location":"' . __( 'Checkpoint', 'fast-events' ) . ' 3","date_format":"l, j F H:i"},"MSyf9BVVw0H8XjTp":{"tickets":"","entry":9,"location":"' . __( 'Finish', 'fast-events' ) . '","date_format":"l, j F H:i"}}',
			)
		);

		// Insert seats row; not really needed, just for integrity.
		$wpdb->insert( $wpdb->prefix . 'fe_event_seats', array( 'event_id' => 3 ) );

		// Insert the order.
		$wpdb->insert(
			$wpdb->prefix . 'fe_event_orders',
			array(
				'id'             => 2,
				'uid'            => UniqIds::uniq_order_id(),
				'event_id'       => 3,
				'event_date'     => wp_date( 'Y-m-d', TimeUtil::local_time() + ( 24 * 3600 * 14 ) ) . ' 10:00:00',
				'payment_status' => 'dashboard',
				'created_at'     => wp_date( 'Y-m-d H:i:s' ),
				'name'           => 'John Doe',
				'email'          => 'JohnDoe@JohnDoe12.com',
				'input_fields'   => '{"tickets":[{"name":"Junior","price":"25.00","vat":"21","cflag":1,"count":1},{"name":"Senior","price":"30.00","vat":"21","cflag":1,"count":0}],"order_amount":"25.00","order_vat":"4.34","fields":null,"events":[3],"orders":[2]}',
				'num_tickets'    => 1,
				'total'          => 25.0000,
				'discount'       => 0.0000,
				'ip_address'     => '1.2.3.4',
			)
		);

		// Insert qrcode for 'Junior' Forest to forest ticket.
		$wpdb->insert(
			$wpdb->prefix . 'fe_event_qrcodes',
			array(
				'id'          => 5,
				'order_id'    => 2,
				'event_id'    => 3,
				'ticket_type' => 'Junior',
				'qrcode'      => UniqIds::uniq_id(),
				'created_at'  => wp_date( 'Y-m-d H:i:s' ),
				'amount'      => 25.0000,
			)
		);

		return $post_ids;
	}
}
