<?php

namespace AC_SYNC\Includes\Classes\Sql\Version {
    if (!defined('ABSPATH'))
        exit;

    use AC_SYNC\Includes\Classes\Logger\Action_Wear_Log as Log;
    use AC_SYNC\Includes\Classes\Product\Action_Wear_Product as Product;

    class Action_Wear_Sql_Version_1_2_2 extends Action_Wear_Sql_Version_Abstract implements Action_Wear_Sql_Version_Interface
    {
        public static function versioningScript()
        {
            // Reset onboarding so the client re-enters the setup wizard
            // without losing any configuration, products, or data
            update_option('_ACTIONWEAR_ONBOARDING', 0);
            Log::write("Migration 1.2.2: Reset onboarding to phase 0", Log::INFO, Log::CONTEXT_GENERAL);

            // Repopulate actionwear_categories_product via generateProducts()
            $apikey = get_option('_ACTIONWEAR_APIKEY');
            if (!empty($apikey)) {
                try {
                    @set_time_limit(300);
                    @ini_set('memory_limit', '512M');
                    Product::generateProducts();
                    Log::write("Migration 1.2.2: Repopulated products and categories_product", Log::INFO, Log::CONTEXT_GENERAL);
                } catch (\Throwable $th) {
                    Log::write("Migration 1.2.2: Failed to repopulate - " . $th->getMessage(), Log::ERROR, Log::CONTEXT_GENERAL);
                }
            }
        }
    }
}
