<?php

namespace WPUltimateSecurity\UltimateSecurity\Bootstrap\Lifecycle;

use WPUltimateSecurity\UltimateSecurity\Support\Helpers\Utils;
use WPUltimateSecurity\UltimateSecurity\Support\Helpers\StringHelper;
use WPUltimateSecurity\UltimateSecurity\Support\Helpers\DateHelper;
use WPUltimateSecurity\UltimateSecurity\Support\Helpers\CryptoHelper;
use WPUltimateSecurity\UltimateSecurity\Infrastructure\Persistence\Schema\DatabaseSchema;
use WPUltimateSecurity\UltimateSecurity\Infrastructure\Security\Capabilities;

class Activator
{
    public static function activate()
	{
        self::reopen_crons();
        // Create all plugin tables using centralized schema management
        DatabaseSchema::create_tables();

        // Run migrations to ensure existing tables match baseline schema
        DatabaseSchema::run_migrations();

		update_option('ultimate_security_recent_search', []);
		update_option('ultimate_security_active_sessions', []);

		$ultimate_security_options = Utils::ultimate_security_get_option();
		Utils::ultimate_security_update_option($ultimate_security_options);

       $deactivation_secret_key = StringHelper::random(20);
        $encrypted_key = CryptoHelper::encrypt($deactivation_secret_key);
        if ($encrypted_key) {
            update_option('ultimate_security_plugin_deactivation_secret_key', $encrypted_key);
        } else {
            update_option('ultimate_security_plugin_deactivation_secret_key', $deactivation_secret_key);
        }

		if (!get_option('ultimate_security_installed'))
			update_option('ultimate_security_installed', DateHelper::now());

		// Track activation time for review notice timing
		if (!get_option('ultimate_security_activated_at')) {
			update_option('ultimate_security_activated_at', time());
		}

		add_option('ultimate_security_do_activation_redirect', true);

         // Update DB version in wp_options
        update_option('ultimate_security_db_version', ULTIMATE_SECURITY_DB_VERSION);
        update_option( 'ultimate_security_paused_crons', false );

        // Install granular capabilities for role-based access
        Capabilities::install();

	}

    public static function reopen_crons()
    {
        $free_crons = ['ultimate_security_daily_9am_task', 'ultimate_security_send_update_success_email', 'ultimate_security_change_salt_keys', 'ultimate_security_url_guard_cleanup', 'ultimate_security_rate_limit_cleanup', 'ultimate_security_reset_expired_lockouts'];

        $ultimate_security_crons = get_option('ultimate_security_crons', []);
        
        $crons = [];

        if (!$ultimate_security_crons) {
            return ;
        }

        if (sizeof($ultimate_security_crons)) {
            foreach($ultimate_security_crons as $hook) {
                
                if (strpos($hook['event'], 'ultimate_security') !== 0) {
                    continue;
                }
                if (in_array($hook['event'], $free_crons)) {
                    continue;
                }
                $crons[] = $hook['event'];
                if ( ! wp_next_scheduled( $hook['event'] ) ) {
                    wp_schedule_event( time(), $hook['schedule'], $hook['event'] );
                }
            }
        }
    }
}
