<?php

namespace IGD_PRO;

defined( 'ABSPATH' ) || exit;

class Install {

	/**
	 * Plugin activation logic.
	 *
	 * Performs initial setup, creates tables, adds default settings,
	 * and handles updates if needed.
	 *
	 * @since 1.0.0
	 */
	public static function activate() {
		self::create_default_data();
		self::create_tables();

		// Add redirection flag
		self::add_redirection_flag();
	}

	public static function create_tables() {
		global $wpdb;

		$charset_collate = $wpdb->get_charset_collate();

		// Logs table
		$tables = [
			"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}integrate_google_drive_logs (
			id INT NOT NULL AUTO_INCREMENT,
			shortcode_id INT UNSIGNED DEFAULT NULL,
			type VARCHAR(255) NULL,
			user_id INT NULL,
			file_id TEXT NOT NULL,
			file_type TEXT NULL,
			file_name TEXT NULL,
			page TEXT NULL,
			account_id TEXT NOT NULL,
			created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id)
		) $charset_collate;",

			// Table for storing user selections
			"CREATE TABLE IF NOT EXISTS {$wpdb->prefix}integrate_google_drive_selections (
			id INT UNSIGNED NOT NULL AUTO_INCREMENT,
			shortcode_id INT UNSIGNED DEFAULT NULL,
			user_id INT UNSIGNED DEFAULT NULL,
			email VARCHAR(255) NOT NULL,
    		status TINYINT(1) NOT NULL DEFAULT 0,
			files TEXT DEFAULT NULL,
			message MEDIUMTEXT DEFAULT NULL,
			page TEXT NULL,
			created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
			updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY (id)
		) $charset_collate;"

		];

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

		foreach ( $tables as $table ) {
			dbDelta( $table );
		}

	}

	/**
	 * Deactivate the plugin and clean up scheduled events.
	 */
	public static function deactivate() {
		self::remove_scheduled_events();
	}

	/**
	 * Remove scheduled cron events during deactivation.
	 *
	 * Clears all registered cron events to prevent them from running
	 * after the plugin is deactivated.
	 */
	private static function remove_scheduled_events() {
		$hooks = [
			'igd_sync_interval',
			'igd_restore_sharing_interval',
			'igd_statistics_daily_report',
			'igd_statistics_weekly_report',
			'igd_statistics_monthly_report',
		];

		foreach ( $hooks as $hook ) {
			self::unschedule_event( $hook );
		}
	}

	/**
	 * Unschedule a single cron event.
	 *
	 * @param string $hook The cron hook name.
	 */
	private static function unschedule_event( $hook ) {
		$timestamp = wp_next_scheduled( $hook );

		if ( $timestamp ) {
			wp_unschedule_event( $timestamp, $hook );
		}
	}

	/**
	 * Create default data for the plugin (version, installation time, etc.).
	 */
	private static function create_default_data() {
		update_option( 'igd_pro_version', IGD_PRO_VERSION );

		if ( ! get_option( 'igd_pro_install_time' ) ) {
			update_option( 'igd_pro_install_time', current_time( 'mysql' ) );
		}
	}

	public static function add_redirection_flag() {
		if ( igd_fs()->can_use_premium_code__premium_only() ) {
			update_option( 'igd_do_activation_redirect', true );
		} else {
			update_option( 'igd_pro_do_activation_redirect', true );
		}
	}


}
