<?php
namespace QuizPress;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use QuizPress\Admin\Settings;

class Migration {

	public static function init() {
		$self = new self();
		add_action( 'admin_init', [ $self, 'run_migration' ] );
	}

	public function run_migration() {
		$quizpress_version = get_option( 'quizpress_version' );
		
		// Flash Role
		if ( get_option( 'quizpress_flash_role_management' ) ) {
			$Installer = new Installer();
			$Installer->add_role();
			delete_option( 'quizpress_flash_role_management' );
		}
		// migration question_explanation
		$this->migrate_add_question_explanation_column( $quizpress_version );
		$this->add_guest_id_column( $quizpress_version );
		$this->add_question_category_ids_question_image_column( $quizpress_version );

		// Save Version Number, flash role management and save permalink
		if ( QUIZPRESS_VERSION !== $quizpress_version ) {
			Settings::save_settings();
			update_option( 'quizpress_version', QUIZPRESS_VERSION );
			update_option( 'quizpress_flash_role_management', true );
			update_option( 'quizpress_required_rewrite_flush', Helper::get_time() );
		}
	}

	public function migrate_add_question_explanation_column( $quizpress_version ) {
		if ( ! version_compare( $quizpress_version, '1.2', '<=' ) ) {
			return;
		}
		global $wpdb;

		$table_name = $wpdb->prefix . 'quizpress_questions';

		// Check if column exists
		$column_exists = $wpdb->get_results(
			$wpdb->prepare(
				"SHOW COLUMNS FROM `$table_name` LIKE %s",
				'question_explanation'
			)
		);

		// If column does not exist → add it
		if ( empty( $column_exists ) ) {
			$wpdb->query(
				"ALTER TABLE `$table_name`
				ADD `question_explanation` LONGTEXT NULL 
				AFTER `question_content`"
			);
		}
	}

	public function add_guest_id_column( $quizpress_version ) {
		if ( ! version_compare( $quizpress_version, '1.3.3', '<=' ) ) {
			return;
		}
		global $wpdb;

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';
		$prefix          = $wpdb->prefix;
		$charset_collate = $wpdb->get_charset_collate();
		Database\CreateGuestTokenTable::up( $prefix, $charset_collate );

		$table_name = $wpdb->prefix . 'quizpress_attempts';

		// Check if column exists
		$column_exists = $wpdb->get_results(
			$wpdb->prepare(
				"SHOW COLUMNS FROM `$table_name` LIKE %s",
				'guest_id'
			)
		);

		// Add column if missing
		if ( empty( $column_exists ) ) {
			$wpdb->query(
				"ALTER TABLE {$table_name}
				ADD guest_id VARCHAR(64) NULL
				AFTER user_id;"
			);
		}
	}

	public function add_question_category_ids_question_image_column( $quizpress_version ) {
		// The 1.3.3 attempt failed on MySQL ( MariaDB-only syntax ), so run for any
		// version below 1.3.4 to add the columns via the fixed dbDelta path.
		if ( ! version_compare( $quizpress_version, '1.3.4', '<' ) ) {
			return;
		}

		global $wpdb;

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

		// dbDelta diffs the live table against the canonical schema and adds any
		// missing columns ( e.g. category_ids, question_image ) on its own. It is
		// idempotent and MySQL/MariaDB-safe, so no hand-written ALTER is needed
		// ( a raw `ADD COLUMN IF NOT EXISTS` is MariaDB-only and breaks on MySQL ).
		Database\CreateQuestionsTable::up( $wpdb->prefix, $wpdb->get_charset_collate() );
	}

}
