<?php
/**
 * Plugin activator.
 *
 * Runs once when the plugin is activated. Validates PHP and WordPress
 * versions, then seeds default options if this is a fresh install.
 *
 * @package VMPlayer
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Class VMPlayer_Activator
 */
class VMPlayer_Activator {

    /**
     * Main activation routine.
     *
     * @return void
     */
    public static function activate() {
        self::check_requirements();

        // Load the Settings class so we can use its default value helpers.
        require_once VMPLAYER_PLUGIN_DIR . 'includes/class-vmplayer-settings.php';

        self::seed_default_settings();
        self::seed_default_stations();

        update_option( 'vmplayer_version', VMPLAYER_VERSION );

        flush_rewrite_rules();
    }

    /**
     * Verify PHP and WordPress version requirements.
     * Aborts activation with a clear error if either fails.
     *
     * @return void
     */
    private static function check_requirements() {
        // Ensure plugin admin functions are available.
        if ( ! function_exists( 'deactivate_plugins' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        // PHP version check.
        if ( version_compare( PHP_VERSION, VMPLAYER_MIN_PHP, '<' ) ) {
            deactivate_plugins( VMPLAYER_PLUGIN_BASENAME );
            wp_die(
                sprintf(
                    /* translators: 1: minimum PHP version, 2: current PHP version */
                    esc_html__( 'VMPlayer requires PHP version %1$s or higher. Your server is running PHP %2$s. Please upgrade PHP and try again.', 'vmplayer' ),
                    esc_html( VMPLAYER_MIN_PHP ),
                    esc_html( PHP_VERSION )
                ),
                esc_html__( 'Plugin Activation Error', 'vmplayer' ),
                array( 'back_link' => true )
            );
        }

        // WordPress version check.
        if ( version_compare( get_bloginfo( 'version' ), VMPLAYER_MIN_WP, '<' ) ) {
            deactivate_plugins( VMPLAYER_PLUGIN_BASENAME );
            wp_die(
                sprintf(
                    /* translators: 1: minimum WP version, 2: current WP version */
                    esc_html__( 'VMPlayer requires WordPress version %1$s or higher. Your site is running WordPress %2$s. Please upgrade WordPress and try again.', 'vmplayer' ),
                    esc_html( VMPLAYER_MIN_WP ),
                    esc_html( get_bloginfo( 'version' ) )
                ),
                esc_html__( 'Plugin Activation Error', 'vmplayer' ),
                array( 'back_link' => true )
            );
        }
    }

    /**
     * Seed default global settings if the option does not exist.
     * Defaults come from the Settings class so we have one source of truth.
     * Existing settings are preserved on re-activation.
     *
     * @return void
     */
    private static function seed_default_settings() {
        if ( false !== get_option( VMPlayer_Settings::OPTION_SETTINGS ) ) {
            return;
        }

        add_option(
            VMPlayer_Settings::OPTION_SETTINGS,
            VMPlayer_Settings::get_default_settings()
        );
    }

    /**
     * Seed default stations array if the option does not exist.
     * Fresh installs get an empty array. Admin will prompt the user
     * to add their first station.
     *
     * @return void
     */
    private static function seed_default_stations() {
        if ( false !== get_option( VMPlayer_Settings::OPTION_STATIONS ) ) {
            return;
        }

        add_option( VMPlayer_Settings::OPTION_STATIONS, array() );
    }
}
