<?php
namespace WooNooW\Core;

use WooNooW\Core\Features;
use WooNooW\Admin\Menu;
use WooNooW\Admin\Assets;
use WooNooW\Admin\StandaloneAdmin;
use WooNooW\Compat\HideWooMenus;
use WooNooW\Compat\MenuProvider;
use WooNooW\Compat\AddonRegistry;
use WooNooW\Compat\RouteRegistry;
use WooNooW\Compat\NavigationRegistry;
use WooNooW\Compat\PaymentChannels;
use WooNooW\Compat\SettingsProvider;
use WooNooW\Compat\MetaFieldsRegistry;
use WooNooW\Admin\Rest\MenuController;
use WooNooW\Admin\Rest\SettingsController;
use WooNooW\Api\Routes;
use WooNooW\Core\Mail\MailQueue;
use WooNooW\Core\Mail\WooEmailOverride;
use WooNooW\Core\DataStores\OrderStore;
use WooNooW\Core\MediaUpload;
use WooNooW\Core\Notifications\PushNotificationHandler;
use WooNooW\Core\Notifications\EmailManager;
use WooNooW\Core\Campaigns\CampaignManager;
use WooNooW\Core\ActivityLog\ActivityLogTable;
use WooNooW\Branding;
use WooNooW\Frontend\Assets as FrontendAssets;
use WooNooW\Frontend\Shortcodes;
use WooNooW\Frontend\TemplateOverride;
use WooNooW\Frontend\PageAppearance;

class Bootstrap {
    public static function init() {
        Features::init();
        HideWooMenus::init();
        Menu::init();
        Assets::init();
        StandaloneAdmin::init();
        Branding::init();
        MediaUpload::init();
        PushNotificationHandler::init();
        EmailManager::instance(); // Initialize custom email system
        CampaignManager::init(); // Initialize campaigns CPT
        
        // Frontend (customer-spa)
        FrontendAssets::init();
        // Note: Shortcodes removed - WC pages now redirect to SPA routes via TemplateOverride
        TemplateOverride::init();
        new PageAppearance();
        
        // Activity Log
        ActivityLogTable::create_table();
        
        // Addon system (order matters: Registry → Routes → Navigation)
        AddonRegistry::init();
        RouteRegistry::init();
        NavigationRegistry::init();
        PaymentChannels::init();
        
        // Level 1 compatibility: Meta fields registry
        MetaFieldsRegistry::init();
        
        MenuProvider::init();
        MenuController::init();
        SettingsProvider::init();
        Routes::init();
        MailQueue::init();
        WooEmailOverride::init();
        OrderStore::init();
        
        // Initialize cart for REST API requests
        add_action('woocommerce_init', [self::class, 'init_cart_for_rest_api']);
        
        // Load custom variation attributes for WooCommerce admin
        add_action('woocommerce_product_variation_object_read', [self::class, 'load_variation_attributes']);
    }
    
    /**
     * Properly initialize WooCommerce cart for REST API requests
     * This is the recommended approach per WooCommerce core team
     */
    public static function init_cart_for_rest_api() {
        // Only load cart for REST API requests
        if (!WC()->is_rest_api_request()) {
            return;
        }
        
        // Load frontend includes (required for cart)
        WC()->frontend_includes();
        
        // Load cart using WooCommerce's official method
        if (null === WC()->cart && function_exists('wc_load_cart')) {
            wc_load_cart();
        }
    }
    
    /**
     * Load custom variation attributes from post meta for WooCommerce admin
     * This ensures WooCommerce's native admin displays custom attributes correctly
     */
    public static function load_variation_attributes($variation) {
        if (!$variation instanceof \WC_Product_Variation) {
            return;
        }
        
        $parent = wc_get_product($variation->get_parent_id());
        if (!$parent) {
            return;
        }
        
        $attributes = [];
        foreach ($parent->get_attributes() as $attr_name => $attribute) {
            if (!$attribute->get_variation()) {
                continue;
            }
            
            // Read from post meta (stored as lowercase)
            $meta_key = 'attribute_' . strtolower($attr_name);
            $value = get_post_meta($variation->get_id(), $meta_key, true);
            
            if (!empty($value)) {
                $attributes[strtolower($attr_name)] = $value;
            }
        }
        
        if (!empty($attributes)) {
            $variation->set_attributes($attributes);
        }
    }
}