<?php
/**
 * ExploreXR Premium Update Checker
 * 
 * Handles automatic updates from private update server using Plugin Update Checker library.
 * 
 * @package ExploreXR_Premium
 * @since 1.0.9
 */

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

/**
 * ExploreXR Update Checker Class
 * 
 * Initializes and manages plugin updates from update.expoxr.com
 */
class ExploreXR_Updater {
    
    /**
     * Update checker instance
     * 
     * @var \YahnisElsts\PluginUpdateChecker\v5p6\Plugin\UpdateChecker|null
     */
    private $update_checker;
    
    /**
     * Plugin file path
     * 
     * @var string
     */
    private $plugin_file;
    
    /**
     * JSON metadata URL
     * 
     * @var string
     */
    private $metadata_url;
    
    /**
     * Plugin slug
     * 
     * @var string
     */
    private $plugin_slug;
    
    /**
     * Constructor
     * 
     * @param string $plugin_file Full path to main plugin file
     * @param string $metadata_url URL to JSON metadata
     * @param string $plugin_slug Plugin slug for update checker
     */
    public function __construct($plugin_file, $metadata_url, $plugin_slug) {
        $this->plugin_file = $plugin_file;
        $this->metadata_url = $metadata_url;
        $this->plugin_slug = $plugin_slug;
        
        // Only initialize in admin context
        if (is_admin()) {
            add_action('admin_init', array($this, 'init_update_checker'));
        }
    }
    
    /**
     * Initialize the update checker
     * 
     * Loads the Plugin Update Checker library and sets up the update checker instance.
     */
    public function init_update_checker() {
        // Load the Plugin Update Checker library
        $this->load_library();
        
        $factory_class = $this->get_factory_class();
        // Check if the library loaded successfully
        if ($factory_class === null) {
            return;
        }
        
        try {
            // Initialize update checker
            $this->update_checker = $factory_class::buildUpdateChecker(
                $this->metadata_url,
                $this->plugin_file,
                $this->plugin_slug
            );

        } catch (Exception $e) {
            // Silently handle errors to avoid breaking admin
        }
    }
    
    /**
     * Load Plugin Update Checker library
     * 
     * Uses safe loading to avoid conflicts if multiple plugins use the same library.
     */
    private function load_library() {
        // Path to vendored library (plugin_file is the main plugin file, lib is in same directory)
        $lib_path = dirname($this->plugin_file) . '/lib/plugin-update-checker/plugin-update-checker.php';
        
        // Check if file exists
        if (!file_exists($lib_path)) {
            return;
        }
        
        // Guard against redeclaration if another plugin already loaded it
        if (!$this->get_factory_class()) {
            require_once $lib_path;
        }
    }

    /**
     * Get the Plugin Update Checker factory class name.
     *
     * @return string|null
     */
    private function get_factory_class() {
        $factory_v5p6 = '\\YahnisElsts\\PluginUpdateChecker\\v5p6\\PucFactory';
        if (class_exists($factory_v5p6)) {
            return $factory_v5p6;
        }

        $factory_v5 = '\\YahnisElsts\\PluginUpdateChecker\\v5\\PucFactory';
        if (class_exists($factory_v5)) {
            return $factory_v5;
        }

        return null;
    }
    
    /**
     * Get update checker instance
     * 
     * @return \YahnisElsts\PluginUpdateChecker\v5p6\Plugin\UpdateChecker|null
     */
    public function get_update_checker() {
        return $this->update_checker;
    }
}
