<?php
namespace ActiriseAdmin\Includes;

use Actirise\Includes\Api;
use Actirise\Includes\Cron;
use Actirise\Includes\EventAnnotation;
use Actirise\Includes\Helpers;
use Actirise\Includes\Options;
use ActirisePublic\Includes\AdsTxt;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * The admin-specific functionality of the plugin.
 *
 * This class executes the migrations of the plugin.
 *
 * @link       https://actirise.com
 * @since      2.4.0
 * @package    actirise
 * @subpackage actirise/admin/includes
 * @author     actirise <wordpress@actirise.com>
 */
final class Migrations {
	/**
	 * The ID of this plugin.
	 *
	 * @since    2.4.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The current version of the plugin.
	 *
	 * @since    2.4.0
	 * @access   protected
	 * @var      string    $version    The current version of the plugin.
	 */
	protected $version;

	/**
	 * The migrations of the plugin.
	 *
	 * @since    2.4.0
	 * @access   private
	 * @var      array<string, array<int, string>>    $migrations    The migrations of the plugin.
	 */
	private $migrations = array(
		'2.4.0' => array(
			'actirise_migrate_240',
		),
		'2.5.1' => array(
			'actirise_migrate_251',
		),
		'2.5.3' => array(
			'actirise_migrate_253',
		),
		'2.5.5' => array(
			'actirise_migrate_255',
		),
		'2.5.6' => array(
			'actirise_migrate_256',
		),
		'2.6.0' => array(
			'actirise_migrate_260',
		),
		'2.6.3' => array(
			'actirise_migrate_263',
		),
		'2.6.5' => array(
			'actirise_migrate_265',
		),
		'2.6.7' => array(
			'actirise_migrate_267',
		),
		'2.6.9' => array(
			'actirise_migrate_269',
		),
		'3.0.0' => array(
			'actirise_migrate_300',
		),
		'3.0.2' => array(
			'actirise_migrate_302',
		),
		'3.2.0' => array(
			'actirise_migrate_320',
		),
		'3.2.2' => array(
			'actirise_migrate_322',
		),
	);

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    2.4.0
	 * @param    string $plugin_name          The name of this plugin.
	 * @param    string $version              The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {
		$this->plugin_name = $plugin_name;
		$this->version     = $version;
	}

	/**
	 * Run the migrations of the plugin.
	 *
	 * @since    2.4.0
	 * @return void
	 */
	public function migrate() {
		if ( wp_doing_ajax() ) {
			return;
		}

		/** @var array<string> $migration */
		$migration = Options::get( 'migrations', array() );
		/** @var string $last_version */
		$last_version = Options::get( 'version', '0.0.0' );
		$updated      = false;

		foreach ( $this->migrations as $version => $methods ) {
			if ( ! isset( $migration[ $version ] ) ) {
				foreach ( $methods as $method ) {
					$this->$method();
				}

				$migration[ $version ] = true;
				$updated               = true;
			}
		}

		if ( $updated === true ) {
			Options::update( 'migrations', $migration );
		}

		if ( $this->version !== $last_version ) {
			Options::update( 'version', $this->version );
			Options::update( 'version-lastupdate', time() );

			if ( $last_version !== '0.0.0' ) {
				EventAnnotation::add_event(
					'Plugin update to v' . $this->version,
					'The plugin was updated from version ' . $last_version . ' to version ' . $this->version,
					'website_update',
					$last_version,
					$this->version,
					true
				);
			}
		}
	}

	/**
	 * Migrate to version 2.4.0
	 *
	 * @since    2.4.0
	 * @return void
	 */
	private function actirise_migrate_240() {
		if ( Options::get( 'adstxt-active', false ) !== true || Options::get( 'adstxt-actirise', '' ) === '' ) {
			return;
		}

		$path_ads_txt = ABSPATH . 'ads.txt';

		if ( file_exists( $path_ads_txt ) ) {
			return;
		}

		AdsTxt::update_file();
	}

	/**
	 * Migrate to version 2.5.1
	 *
	 * @since    2.5.1
	 * @return void
	 */
	private function actirise_migrate_251() {
		$old_logs = get_option( $this->plugin_name . '_logs', false );

		if ( $old_logs !== false ) {
			Options::update( 'logs', $old_logs );
			delete_option( $this->plugin_name . '_logs' );
		}
	}

	/**
	 * Migrate to version 2.5.3
	 *
	 * @since    2.5.3
	 * @return void
	 */
	private function actirise_migrate_253() {
		delete_option( 'actirise-api_version' );
		delete_option( 'actirise-api-lastupdate' );

		wp_clear_scheduled_hook( 'actirise_cron_get_api_version' );
	}

	/**
	 * Migrate to version 2.5.5
	 *
	 * @since 2.5.5
	 * @return void
	 */
	private function actirise_migrate_255() {
		delete_option( 'actirise-debug-last-update' );
		wp_clear_scheduled_hook( 'actirise_cron_update_debug_token' );
	}

	/**
	 * Migrate to version 2.5.6
	 *
	 * @since 2.5.6
	 * @return void
	 */
	private function actirise_migrate_256() {
		$cron = new Cron();
		$cron->update_adstxt();
	}

	/**
	 * Migrate to version 2.6.0
	 *
	 * @since 2.6.0
	 * @return void
	 */
	private function actirise_migrate_260() {
		$prefix = 'actirise-';

		delete_option( $prefix . 'adstxt-lastupdate' );
		delete_option( $prefix . 'presizeddiv-lastupdate' );
		delete_option( $prefix . 'fastcmp-lastupdate' );
		delete_option( $prefix . 'init-plugin' );

		/** @var \wpdb $wpdb */
		global $wpdb;
		$table_name = $wpdb->prefix . 'actirise_options';

		$options = $wpdb->get_results( // phpcs:ignore
			$wpdb->prepare(
				"SELECT option_name, option_value, autoload FROM $wpdb->options WHERE option_name LIKE %s", // @phpstan-ignore argument.type
				$prefix . '%'
			),
			ARRAY_A
		);

		if ( empty( $options ) ) {
				return;
		}

		foreach ( $options as $option ) {
			$option_name  = substr( $option['option_name'], strlen( $prefix ) ); // @phpstan-ignore argument.type
			$option_value = $option['option_value'];
			$autoload     = 1;

			if ( Options::exists( $option_name ) ) {
				$wpdb->query( // phpcs:ignore
					$wpdb->prepare( // @phpstan-ignore argument.type
						// @phpstan-ignore-next-line argument.type
						"UPDATE $table_name SET option_value = %s, autoload = %d WHERE option_name = %s", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
						$option_value,
						$autoload,
						$option_name
					)
				);
			} else {
				$wpdb->query( // phpcs:ignore
					$wpdb->prepare( // @phpstan-ignore argument.type
						// @phpstan-ignore-next-line argument.type
						"INSERT INTO $table_name (option_name, option_value, autoload) VALUES (%s, %s, %d)", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
						$option_name,
						$option_value,
						$autoload
					)
				);
			}
		}

		if ( ! Options::exists( 'debug-enabled' ) ) {
			Options::update( 'debug-enabled', true );
		}

		$wpdb->query( // phpcs:ignore
			$wpdb->prepare( // @phpstan-ignore argument.type
				"DELETE FROM $wpdb->options WHERE option_name LIKE %s", // @phpstan-ignore argument.type
				$prefix . '%'
			)
		);
	}

	/**
	 * Migrate to version 2.6.3
	 *
	 * @since 2.6.3
	 * @return void
	 */
	private function actirise_migrate_263() {
		if ( ! Options::exists( 'custom1' ) ) {
			Options::update( 'custom1', 'author_ID' );
		}
		if ( ! Options::exists( 'custom2' ) ) {
			Options::update( 'custom2', 'category_0_slug' );
		}
		if ( ! Options::exists( 'custom3' ) ) {
			Options::update( 'custom3', 'post_ID' );
		}
		if ( ! Options::exists( 'custom4' ) ) {
			Options::update( 'custom4', '' );
		}
		if ( ! Options::exists( 'custom5' ) ) {
			Options::update( 'custom5', '' );
		}
	}

	/**
	 * Migrate to version 2.6.5
	 *
	 * @since 2.6.5
	 * @return void
	 */
	private function actirise_migrate_265() {
		Options::update( 'cta-cache-200', false );
	}

	/**
	 * Migrate to version 2.6.7
	 *
	 * @since 2.6.7
	 * @return void
	 */
	private function actirise_migrate_267() {
		Options::update( 'cta-cache-200', false );
	}

	/**
	 * Migrate to version 2.6.9
	 *
	 * @since 2.6.9
	 * @return void
	 */
	private function actirise_migrate_269() {
		Options::update( 'fastcmp-consentV2', true );
		Options::update( 'fastcmp-googleTagsStatus', false );
		Options::update( 'fastcmp-googleAdStorage', false );
		Options::update( 'fastcmp-googleAnalyticsStorage', false );
		Options::update( 'fastcmp-googlePersonalizationStorage', false );
		Options::update( 'fastcmp-googleFunctionalityStorage', false );
		Options::update( 'fastcmp-googleSecurityStorage', false );
		Options::update( 'fastcmp-googleAdPersonalization', false );
		Options::update( 'fastcmp-googleAdUserData', false );
	}

	/**
	 * Migrate to version 3.0.0
	 *
	 * @since 3.0.0
	 * @return void
	 */
	private function actirise_migrate_300() {
		$options_to_migrate = array(
			'adstxt-active',
			'adstxt-update',
			'adstxt-file',
			'presizeddiv-active',
		);

		foreach ( $options_to_migrate as $option_name ) {
			$option_value = Options::get( $option_name );

			if ( $option_value === 'true' ) {
					Options::update( $option_name, true );
			} elseif ( $option_value === 'false' ) {
					Options::update( $option_name, false );
			}
		}

		if ( is_multisite() ) {
			AdsTxt::delete_file();
		}
	}

	/**
	 * Migrate to version 3.0.2
	 *
	 * @since 3.0.2
	 * @return void
	 */
	private function actirise_migrate_302() {
		Core::check_website_status();
	}

	/**
	 * Migrate to version 3.2.0
	 *
	 * @since 3.2.0
	 * @return void
	 */
	private function actirise_migrate_320() {
		if ( Options::get( 'settings-analytics-token', '' ) !== '' && Options::get( 'settings-analytics-userid', '' ) === '' ) {
			Helpers::get_user_id();
		}

		if ( Options::get( 'presizeddiv-init' ) !== true ) {
			Options::delete( 'presizeddiv-init' );
		}
	}

	/**
	 * Migrate to version 3.2.2
	 *
	 * @since 3.3
	 * @return void
	 */
	private function actirise_migrate_322() {
		if ( Options::exists( 'cta-cache-200' ) ) {
			Options::delete( 'cta-cache-200' );
		}
	}
}
