<?php
/**
 * Plugin Activator
 * 
 * Fired during plugin activation
 * 
 * @package VariationHub
 * @since 1.0.0
 */

namespace VariationHub;

defined('ABSPATH') || exit;

class Activator {
    
    /**
     * Activation hook
     */
    public static function activate() {
        // Check requirements
        if (!self::check_requirements()) {
            return;
        }
        
        // Create cache table
        Core\Cache_Table::create_table();

        // Schedule initial sync (background)
        Core\Cache_Sync::schedule_full_sync();

        // Schedule daily license validation
        Core\License_Manager::schedule_validation();

        // Set activation flag
        update_option('varihu_activated', current_time('mysql'));
        
        // Flush rewrite rules
        flush_rewrite_rules();
    }
    
    /**
     * Check system requirements
     */
    private static function check_requirements() {
        $errors = [];
        
        // PHP version
        if (version_compare(PHP_VERSION, '7.4', '<')) {
            $errors[] = 'PHP 7.4 or higher required';
        }
        
        // WooCommerce
        if (!class_exists('WooCommerce')) {
            $errors[] = 'WooCommerce plugin required';
        }
        
        // MySQL version
        global $wpdb;
        $mysql_version = $wpdb->db_version();
        if (version_compare($mysql_version, '5.7', '<')) {
            $errors[] = 'MySQL 5.7 or higher required (for JSON support)';
        }
        
        if (!empty($errors)) {
            deactivate_plugins(VARIHU_PLUGIN_BASENAME);
            wp_die(
                '<h1>Variation Hub Activation Error</h1>' .
                '<ul><li>' . implode('</li><li>', array_map('esc_html', $errors)) . '</li></ul>' .
                '<p><a href="' . esc_url(admin_url('plugins.php')) . '">Back to Plugins</a></p>'
            );
            return false;
        }
        
        return true;
    }
}
