<?php
declare(strict_types=1);

namespace EHxMe\Classes;

use EHxMe\Database\DBMigrator;
use EHxMe\Helpers\Helper;
use EHxMe\Services\Request;

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

class ActivationHandler
{    
    const ACTIVATION_OPTION_KEY = 'ehxme_activation_redirect';

    /**
     * Constructor for the ActivationHandler class.
     *
     * Initializes the sets up WordPress hooks.
     *
     * @return void
     */
    public function __construct()
    {
        add_action( 'activated_plugin', function($plugin) {
            if ( $plugin === plugin_basename( __FILE__ ) ) {
                update_option(self::ACTIVATION_OPTION_KEY, true);
            }
        });

        add_action('admin_init', [$this, 'redirectAfterActivation']);
    }

    /**
     * Plugin activation hook.
     *
     * This function is called when the plugin is activated. It performs the following tasks:
     * 1. Updates the rewrite rules.
     * 2. Creates the payment table if it doesn't exist.
     * 3. Sets default plugin options.
     *
     * @return void
     */
    public static function handle()
    {
        DBMigrator::run();

        self::setPluginInstallTime();
    }
    
    /**
     * Set plugin install time
     *
     * @return void
     */
    public static function setPluginInstallTime()
    {
        // $data = get_option( 'wp_statuses', []);
        // if( !isset($data['installed_time']) ){
        //     $data['installed_time'] = strtotime("now") ;
        //     update_option('wp_statuses', $data, false);
        // }

        // Add custom roles using the Helper::customRoles() method
        foreach (Helper::customRoles() as $key => $value) {
            add_role($key, $value);
        }

        // Define default payment gateway settings
        $options = [
            'admin_email_address' => 'example@eh.studio',
            'mail_appears_from' => 'EHx Studio',
            'mail_appears_from_address' => 'example@eh.studio',
            'content_type' => true,
            'stripe_test_mode_enable' => true,
            'stripe_enable' => true,
            'stripe_client_key' => 'pk_test_51R3tRbCo429twQWUFnIVnK8K0tH9Z1enVNk5Pggn3cABcgqctnO01kj60811kPBVLuSERJXphpfSzabb4CUWdrlb00ynOqC7Ot',
            'stripe_client_secret' => 'sk_test_51R3tRbCo429twQWUYCwaeYwTJFPGj2VPaaGDdawemLCojNAvttxquBmhbUGbFNuALznNhw4KdZ11MdatryMjZVSQ00hCKZNEiK',
        ];

        // Update the plugin's options with the default settings
        update_option(Settings::$option, $options);

        update_option( self::ACTIVATION_OPTION_KEY, true );
    }

    /**
     * Redirect After Activation
     *
     * @return void
     */
    public function redirectAfterActivation() 
    {
        // Check if the redirect option is set
        if ( get_option( self::ACTIVATION_OPTION_KEY, false ) ) {
            
            // Delete the option to prevent future redirects
            delete_option( self::ACTIVATION_OPTION_KEY);
            
            if(Request::getInput('activate') == 'true') {
                $page = AdminMenuHandler::$pages['setting'];
                wp_safe_redirect(admin_url("admin.php?page=$page"));
                exit;
            }
        }
    }
}