<?php
/**
 * AllAccessible Version Manager
 *
 * Handles version checking and upgrade routines
 *
 * @package     AllAccessible
 * @since       1.3.7
 */

if (!defined('ABSPATH')) {
    die('You are not allowed to call this page directly.');
}

class AllAccessible_VersionManager {
    
    /**
     * Initialize the version manager
     */
    public static function init() {
        add_action('plugins_loaded', array(__CLASS__, 'check_version'));
    }
    
    /**
     * Check if plugin version has changed and run upgrade routines if needed
     */
    public static function check_version() {
        $installed_version = get_option('aacb_version', '0');
        
        // If version has changed, run upgrade routines
        if (version_compare($installed_version, AACB_VERSION, '<')) {
            self::run_upgrade_routines($installed_version);
            
            // Update stored version number
            update_option('aacb_version', AACB_VERSION);
        }
    }
    
    /**
     * Run version-specific upgrade routines
     * 
     * @param string $from_version The previously installed version
     */
    private static function run_upgrade_routines($from_version) {
        // Example upgrade routine for version 1.3.0
        if (version_compare($from_version, '1.3.0', '<')) {
            self::upgrade_to_1_3_0();
        }
        
        // Example upgrade routine for version 1.3.5
        if (version_compare($from_version, '1.3.5', '<')) {
            self::upgrade_to_1_3_5();
        }
        
        // Add future upgrade routines here
        if (version_compare($from_version, '1.3.7', '<')) {
            self::upgrade_to_1_3_7();
        }

        // Upgrade to 2.0.0 - Handle wizard migration
        if (version_compare($from_version, '2.0.0', '<')) {
            self::upgrade_to_2_0_0();
        }

        // 2.1.0 — consolidated migration for everything between 2.0.x
        // and the public 2.1.0 release.

        // All four actions are idempotent + cheap. No-op for fresh
        // installs (only fires when from_version < 2.1.0).
        if (version_compare($from_version, '2.1.0', '<')) {
            self::upgrade_to_2_1_0();
        }

        // 2.1.4 — flip the plugin signing secret off autoload. New fetches
        // already store it with autoload=off, but rows created by earlier
        // versions stay autoloaded (WP autoloads small values), so the
        // secret rides every request until the next re-fetch. Flip the
        // installed base immediately on upgrade.
        if (version_compare($from_version, '2.1.4', '<')) {
            self::upgrade_to_2_1_4();
        }

        // Always run this to ensure database is up to date
        self::update_db_check();
    }

    /**
     * Upgrade routine for version 2.1.0 — see consolidated comment in
     * run_upgrade_routines() above.
     */
    private static function upgrade_to_2_1_0() {
        if (class_exists('AllAccessible_ApiClient')) {
            $client = AllAccessible_ApiClient::get_instance();
            try { $client->invalidate_site_resolution(); } catch (\Throwable $e) { /* swallow */ }
            if (method_exists($client, 'bust_manifest_caches')) {
                try { $client->bust_manifest_caches(); } catch (\Throwable $e) { /* swallow */ }
            }
        } else {
            // Defensive: invalidate site resolution directly if ApiClient
            // hasn't loaded yet at this hook firing.
            delete_transient('aacb_site_options_cache');
            delete_option('aacb_siteID');
        }

        if (class_exists('AllAccessible_PostLinkBackfill')) {
            try { AllAccessible_PostLinkBackfill::on_activate(); } catch (\Throwable $e) { /* swallow */ }
        }

        // Image grid cache stamp — bump even if the helpers above didn't
        // load. Default is 1, increment past it so the version-stamp
        // cache key changes and next Image Manager render misses cache.
        $current = (int) get_option('aacb_image_grid_cache_stamp', 1);
        update_option('aacb_image_grid_cache_stamp', $current + 1, false);
    }
    
    /**
     * Upgrade routine for version 1.3.0
     */
    private static function upgrade_to_1_3_0() {
        // Example: Add new option
        // add_option('aacb_new_feature_enabled', true);
    }
    
    /**
     * Upgrade routine for version 1.3.5
     */
    private static function upgrade_to_1_3_5() {
        // Example: Security improvements
        // delete_option('aacb_deprecated_setting');
    }
    
    /**
     * Upgrade routine for version 1.3.7
     */
    private static function upgrade_to_1_3_7() {
        // Migrate to new version management system
        $options = get_option('aacb_options', array());
        if (!empty($options) && !isset($options['version'])) {
            $options['version'] = AACB_VERSION;
            update_option('aacb_options', $options);
        }
    }

    /**
     * Upgrade routine for version 2.0.0
     * Handles migration from 1.x to 2.0 - new wizard system
     */
    private static function upgrade_to_2_0_0() {
        $account_id = get_option('aacb_accountID');

        // If user already has accountID (upgraded from 1.x), mark wizard as completed
        if (!empty($account_id)) {
            update_option('aacb_wizard_completed', true);

            // Set tier to 'unknown' since we don't know if they were free or premium in 1.x
            // They can continue using their existing account
            if (!get_option('aacb_account_tier')) {
                update_option('aacb_account_tier', 'legacy');
            }
        }

        // Clean up old options that are no longer used in 2.0
        delete_option('aacb_engagement_score');
        delete_option('aacb_widget_opens_count');
        delete_option('aacb_days_since_install');
        delete_option('aacb_conversion_events');
        delete_option('aacb_email_capture_shown');
        delete_option('aacb_email_capture_count');
    }

    /**
     * Upgrade routine for version 2.1.4 — move the signing secret off
     * autoload on existing installs. Re-add each row with autoload='no'
     * (delete+add, since update_option won't change an existing row's
     * autoload flag). Idempotent and safe if the rows are absent.
     */
    private static function upgrade_to_2_1_4() {
        $keys = array('aacb_plugin_secret', 'aacb_plugin_secret_canon', 'aacb_plugin_secret_version');
        foreach ($keys as $key) {
            $val = get_option($key, null);
            if ($val === null) {
                continue;
            }
            delete_option($key);
            add_option($key, $val, '', 'no');
        }
    }

    /**
     * Update database check
     */
    private static function update_db_check() {
        // This is where you would run any database schema updates if needed
    }
}

// Initialize the version manager
AllAccessible_VersionManager::init();
