<?php

namespace SGC\eGiftCard\Hooks\Handler;
use SGC\eGiftCard\Database\DBMigrate;

defined('ABSPATH') || exit; // Prevent direct file access.

/**
 * Class ActivationHandler
 *
 * Handles plugin activation tasks.
 */
class ActivationHandler
{
    /**
     * Handle plugin activation.
     *
     * This method is called when the plugin is activated.
     * It runs database migrations and updates the plugin version option.
     */
    public function handle()
    {
        // Run database migrations.
        DBMigrate::run();

        // Activate the plugin.
        $this->activate();

        if (! wp_next_scheduled('smart_gift_card_hourly_tasks')) {
            wp_schedule_event(time(), 'hourly', 'smart_gift_card_hourly_tasks');
        }

        if (! wp_next_scheduled('smart_gift_card_daily_tasks')) {
            wp_schedule_event(time(), 'daily', 'smart_gift_card_daily_tasks');
        }
    }

    /**
     * Activate the plugin.
     *
     * Sets the plugin version option if it doesn't exist.
     */
    public function activate()
    {
        $version = get_option('smart_gift_card_plugin_version');

        // Check if the plugin version option doesn't exist and set it.
        if (!$version) {
            update_option('smart_gift_card_plugin_version', SMART_GIFT_CARD_VERSION);
        }
    }
}