<?php

defined('ABSPATH') || exit;

class BundlePress_Customization_API
{
    private $namespace;
    private static $cache = null;

    public function __construct($namespace = 'bundlepress/v1')
    {
        $this->namespace = $namespace;
        add_action('rest_api_init', [$this, 'register_routes']);
    }

    public function register_routes()
    {
        register_rest_route($this->namespace, '/customizations', [
            'methods' => 'GET',
            'callback' => [$this, 'get_customizations'],
            'permission_callback' => [$this, 'check_permission'],
        ]);

        register_rest_route($this->namespace, '/customizations', [
            'methods' => 'POST',
            'callback' => [$this, 'update_customizations'],
            'permission_callback' => [$this, 'check_permission'],
        ]);
    }

    public function check_permission()
    {
        return current_user_can('manage_options');
    }

    public function get_customizations($request)
    {
        return rest_ensure_response([
            'success' => true,
            'data' => self::get_customizations_data(),
        ]);
    }

    public function update_customizations($request)
    {
        global $wpdb;
        $table = $wpdb->prefix . 'bundlepress_customizations';

        $params = $request->get_json_params();
        if (!isset($params['customizations']) || !is_array($params['customizations'])) {
            return new WP_Error('missing_customizations', 'Customizations data is required', ['status' => 400]);
        }

        $customizations = $params['customizations'];

        // Check if data is already flat (has keys like volume_heading) or nested (has keys like volume)
        $is_flat = isset($customizations['volume_heading']) || isset($customizations['mixmatch_heading']);

        if ($is_flat) {
            $flat = $customizations;
        } else {
            $flat = self::flatten_customizations($customizations);
        }

        $allowed = array_keys(self::get_default_flat());
        foreach ($flat as $key => $value) {
            if (!in_array($key, $allowed, true)) {
                // Skip invalid keys silently instead of returning error
                continue;
            }
            if (!is_string($value) && !is_bool($value)) {
                return new WP_Error('invalid_customization', "Invalid customization value type for: $key", ['status' => 400]);
            }
        }

        foreach ($allowed as $key) {
            $raw_val = isset($flat[$key]) ? $flat[$key] : self::get_default_flat()[$key];
            
            if (is_bool($raw_val)) {
                $val = $raw_val ? '1' : '0';
            } else {
                $val = sanitize_text_field($raw_val);
            }

            $exists = $wpdb->get_var(
                $wpdb->prepare(
                    "SELECT COUNT(*) FROM {$wpdb->prefix}bundlepress_customizations WHERE customization_key = %s",
                    $key
                )
            );

            if ((int)$exists > 0) {
                $wpdb->update(
                    $table,
                    ['customization_value' => wp_json_encode($val)],
                    ['customization_key' => $key],
                    ['%s'],
                    ['%s']
                );
            } else {
                $wpdb->insert(
                    $table,
                    [
                        'customization_key' => $key,
                        'customization_value' => wp_json_encode($val),
                    ],
                    ['%s', '%s']
                );
            }
        }

        self::$cache = null;

        return rest_ensure_response([
            'success' => true,
            'message' => 'Customizations updated successfully',
            'data' => self::get_customizations_data(),
        ]);
    }

    public static function get_customizations_data()
    {
        if (self::$cache !== null) {
            return self::$cache;
        }

        $flat = self::get_customizations_flat();

        self::$cache = [
            'volume' => [
                'heading' => $flat['volume_heading'],
                'button_select_deal' => $flat['volume_button_select_deal'],
                'button_choose_variations' => $flat['volume_button_choose_variations'],
                'button_claim_deal' => $flat['volume_button_claim_deal'],
                'placeholder_select' => $flat['volume_placeholder_select'],
                'hover_bg' => $flat['volume_hover_bg'],
                'layout' => $flat['volume_layout'],
                'tier_prefix' => $flat['volume_tier_prefix'],
                'discount_text' => $flat['volume_discount_text'],
                'badge_bg' => $flat['volume_badge_bg'],
                'badge_color' => $flat['volume_badge_color'],
                'button_bg' => $flat['volume_button_bg'],
                'button_color' => $flat['volume_button_color'],
                'show_regular_price' => $flat['volume_show_regular_price'],
                'popular_badge_text' => $flat['volume_popular_badge_text'],
                'popular_badge_bg' => $flat['volume_popular_badge_bg'],
                'popular_badge_color' => $flat['volume_popular_badge_color'],
            ],
            'mixmatch' => [
                'heading' => $flat['mixmatch_heading'],
                'button_claim_deal' => $flat['mixmatch_button_claim_deal'],
                'header_badge_build' => $flat['mixmatch_header_badge_build'],
                'tier_suffix' => $flat['mixmatch_tier_suffix'],
                'discount_text_suffix' => $flat['mixmatch_discount_text_suffix'],
                'hover_bg' => $flat['mixmatch_hover_bg'],
                'card_bg' => $flat['mixmatch_card_bg'],
                'header_color' => $flat['mixmatch_header_color'],
                'header_badge_bg' => $flat['mixmatch_header_badge_bg'],
                'price_color' => $flat['mixmatch_price_color'],
                'regular_price_color' => $flat['mixmatch_regular_price_color'],
                'tier_active_border' => $flat['mixmatch_tier_active_border'],
                'tier_active_text' => $flat['mixmatch_tier_active_text'],
                'tier_discount_color' => $flat['mixmatch_tier_discount_color'],
                'product_name_color' => $flat['mixmatch_product_name_color'],
                'product_price_color' => $flat['mixmatch_product_price_color'],
                'qty_btn_bg' => $flat['mixmatch_qty_btn_bg'],
                'qty_btn_add_color' => $flat['mixmatch_qty_btn_add_color'],
                'items_bar_bg' => $flat['mixmatch_items_bar_bg'],
                'items_bar_text' => $flat['mixmatch_items_bar_text'],
                'button_bg' => $flat['mixmatch_button_bg'],
                'button_color' => $flat['mixmatch_button_color'],
                'items_selected_label' => $flat['mixmatch_items_selected_label'],
            ],
            'fbt' => [
                'heading' => $flat['fbt_heading'],
                'button_add_items' => $flat['fbt_button_add_items'],
                'button_added' => $flat['fbt_button_added'],
                'save_label' => $flat['fbt_save_label'],
                'total_label' => $flat['fbt_total_label'],
                'hover_bg' => $flat['fbt_hover_bg'],
                'card_bg' => $flat['fbt_card_bg'],
                'header_color' => $flat['fbt_header_color'],
                'header_dot_color' => $flat['fbt_header_dot_color'],
                'badge_bg' => $flat['fbt_badge_bg'],
                'badge_color' => $flat['fbt_badge_color'],
                'product_list_bg' => $flat['fbt_product_list_bg'],
                'product_item_bg' => $flat['fbt_product_item_bg'],
                'product_item_border_active' => $flat['fbt_product_item_border_active'],
                'product_name_color' => $flat['fbt_product_name_color'],
                'product_price_color' => $flat['fbt_product_price_color'],
                'total_label_color' => $flat['fbt_total_label_color'],
                'total_price_color' => $flat['fbt_total_price_color'],
                'total_regular_price_color' => $flat['fbt_total_regular_price_color'],
                'button_bg' => $flat['fbt_button_bg'],
                'button_color' => $flat['fbt_button_color'],
            ],
            'bxgy' => [
                'heading' => $flat['bxgy_heading'],
                'button_claim_offer' => $flat['bxgy_button_claim_offer'],
                'button_claimed' => $flat['bxgy_button_claimed'],
                'free_label' => $flat['bxgy_free_label'],
                'free_shipping_label' => $flat['bxgy_free_shipping_label'],
                'hover_bg' => $flat['bxgy_hover_bg'],
                'card_bg' => $flat['bxgy_card_bg'],
                'header_color' => $flat['bxgy_header_color'],
                'header_badge_bg' => $flat['bxgy_header_badge_bg'],
                'header_badge_color' => $flat['bxgy_header_badge_color'],
                'section_title_color' => $flat['bxgy_section_title_color'],
                'item_bg' => $flat['bxgy_item_bg'],
                'item_border' => $flat['bxgy_item_border'],
                'item_name_color' => $flat['bxgy_item_name_color'],
                'item_price_color' => $flat['bxgy_item_price_color'],
                'free_badge_bg' => $flat['bxgy_free_badge_bg'],
                'free_badge_color' => $flat['bxgy_free_badge_color'],
                'free_item_border' => $flat['bxgy_free_item_border'],
                'free_item_bg' => $flat['bxgy_free_item_bg'],
                'button_bg' => $flat['bxgy_button_bg'],
                'button_color' => $flat['bxgy_button_color'],
                'customer_buys_text' => $flat['bxgy_customer_buys_text'],
                'customer_gets_text' => $flat['bxgy_customer_gets_text'],
                'purchased_items_text' => $flat['bxgy_purchased_items_text'],
                'free_shipping_text' => $flat['bxgy_free_shipping_text'],
                'badge_buy_get_free' => $flat['bxgy_badge_buy_get_free'],
                'badge_buy_get_discount' => $flat['bxgy_badge_buy_get_discount'],
                'badge_buy_get_shipping' => $flat['bxgy_badge_buy_get_shipping'],
            ],
            'fixed' => [
                'heading' => $flat['fixed_heading'],
                'button_text' => $flat['fixed_button_text'],
                'qty_label' => $flat['fixed_qty_label'],
                'hover_bg' => $flat['fixed_hover_bg'],
                'layout' => $flat['fixed_layout'],
                'selection_text' => $flat['fixed_selection_text'],
                'product_bg' => $flat['fixed_product_bg'],
                'product_title_color' => $flat['fixed_product_title_color'],
                'product_text_color' => $flat['fixed_product_text_color'],
                'button_bg' => $flat['fixed_button_bg'],
                'button_color' => $flat['fixed_button_color'],
                'show_regular_price' => $flat['fixed_show_regular_price'],
                'price_label' => $flat['fixed_price_label'],
                'variation_bg' => $flat['fixed_variation_bg'],
                'variation_text_color' => $flat['fixed_variation_text_color'],
            ],
            'general' => [
                'cart_success_message' => $flat['general_cart_success_message'],
                'cart_save_text' => $flat['general_cart_save_text'],
                'adding_text' => $flat['general_adding_text'],
                'no_items_text' => $flat['general_no_items_text'],
                'bundle_price_label' => $flat['general_bundle_price_label'],
                'selected_price_label' => $flat['general_selected_price_label'],
                'bundle_placement' => $flat['general_bundle_placement'],
            ],
        ];

        return self::$cache;
    }

    public static function get_customizations_flat()
    {
        $defaults = self::get_default_flat();

        global $wpdb;
        $table = $wpdb->prefix . 'bundlepress_customizations';

        $table_exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table));
        if (!$table_exists) {
            return $defaults;
        }

        $rows = $wpdb->get_results("SELECT customization_key, customization_value FROM {$wpdb->prefix}bundlepress_customizations", ARRAY_A);
        if (!$rows) {
            return $defaults;
        }

        $out = $defaults;
        foreach ($rows as $row) {
            $k = $row['customization_key'];
            if (!isset($out[$k])) continue;
            $decoded = json_decode($row['customization_value'], true);
            if (is_string($decoded)) {
                $out[$k] = $decoded;
            }
        }

        return $out;
    }

    public static function get_default_flat()
    {
        return [
            'volume_heading' => 'Volume Discount',
            'volume_button_select_deal' => 'Select a Deal',
            'volume_button_choose_variations' => 'Choose Variations',
            'volume_button_claim_deal' => 'Claim This Deal',
            'volume_placeholder_select' => 'Select {label}',
            'volume_hover_bg' => '#E3F2FD',
            'volume_layout' => 'grid',
            'volume_tier_prefix' => 'Buy',
            'volume_discount_text' => '{value} OFF',
            'volume_badge_bg' => '#51eb56',
            'volume_badge_color' => '#000000',
            'volume_button_bg' => '#2563EB',
            'volume_button_color' => '#FFFFFF',
            'volume_popular_badge_text' => 'Popular',
            'volume_popular_badge_bg' => '#EF4444',
            'volume_popular_badge_color' => '#FFFFFF',
            'volume_show_regular_price' => true,

            'mixmatch_heading' => 'Mix & Match Offer',
            'mixmatch_button_claim_deal' => 'Claim This Deal',
            'mixmatch_header_badge_build' => 'BUILD YOUR DEAL',
            'mixmatch_tier_suffix' => 'Items',
            'mixmatch_discount_text_suffix' => 'OFF',
            'mixmatch_hover_bg' => '#F3E8FF',
            'mixmatch_card_bg' => '#FFFFFF',
            'mixmatch_header_color' => '#6B21A8',
            'mixmatch_header_badge_bg' => '#805AD5',
            'mixmatch_price_color' => '#1A202C',
            'mixmatch_regular_price_color' => '#9CA3AF',
            'mixmatch_tier_active_border' => '#9F7AEA',
            'mixmatch_tier_active_text' => '#1A202C',
            'mixmatch_tier_discount_color' => '#9F7AEA',
            'mixmatch_product_name_color' => '#374151',
            'mixmatch_product_price_color' => '#6B7280',
            'mixmatch_qty_btn_bg' => '#F3F4F6',
            'mixmatch_qty_btn_add_color' => '#805AD5',
            'mixmatch_items_bar_bg' => '#F9FAFB',
            'mixmatch_items_bar_text' => '#6B7280',
            'mixmatch_button_bg' => '#805AD5',
            'mixmatch_button_color' => '#FFFFFF',
            'mixmatch_items_selected_label' => 'Items Selected:',

            'fbt_heading' => 'Frequently Bought Together',
            'fbt_button_add_items' => 'Add {count} Items to Cart',
            'fbt_button_added' => '✓ Added!',
            'fbt_save_label' => 'SAVE',
            'fbt_total_label' => 'Total Price:',
            'fbt_hover_bg' => '#E3F2FD',
            'fbt_card_bg' => '#FFFFFF',
            'fbt_header_color' => '#1D4ED8',
            'fbt_header_dot_color' => '#3B82F6',
            'fbt_badge_bg' => '#2563EB',
            'fbt_badge_color' => '#FFFFFF',
            'fbt_product_list_bg' => '#F9FAFB',
            'fbt_product_item_bg' => '#FFFFFF',
            'fbt_product_item_border_active' => '#93C5FD',
            'fbt_product_name_color' => '#374151',
            'fbt_product_price_color' => '#374151',
            'fbt_total_label_color' => '#6B7280',
            'fbt_total_price_color' => '#2563EB',
            'fbt_total_regular_price_color' => '#9CA3AF',
            'fbt_button_bg' => '#2563EB',
            'fbt_button_color' => '#FFFFFF',

            'bxgy_heading' => 'Buy X Get Y',
            'bxgy_button_claim_offer' => 'Claim Offer',
            'bxgy_button_claimed' => '✓ Claimed!',
            'bxgy_free_label' => 'FREE',
            'bxgy_free_shipping_label' => 'FREE SHIP',
            'bxgy_hover_bg' => '#F3E8FF',
            'bxgy_card_bg' => '#FFFFFF',
            'bxgy_header_color' => '#805AD5',
            'bxgy_header_badge_bg' => '#3182CE',
            'bxgy_header_badge_color' => '#FFFFFF',
            'bxgy_section_title_color' => '#718096',
            'bxgy_item_bg' => '#FFFFFF',
            'bxgy_item_border' => '#E2E8F0',
            'bxgy_item_name_color' => '#2D3748',
            'bxgy_item_price_color' => '#2D3748',
            'bxgy_free_badge_bg' => '#38A169',
            'bxgy_free_badge_color' => '#FFFFFF',
            'bxgy_free_item_border' => '#9AE6B4',
            'bxgy_free_item_bg' => '#F0FFF4',
            'bxgy_button_bg' => '#805AD5',
            'bxgy_button_color' => '#FFFFFF',
            'bxgy_customer_buys_text' => 'Customer Buys',
            'bxgy_customer_gets_text' => 'Customer Gets',
            'bxgy_purchased_items_text' => 'Purchased Items',
            'bxgy_free_shipping_text' => '🚚 Free shipping on your entire order!',
            'bxgy_badge_buy_get_free' => 'Buy {buy_qty} Get {get_qty} Free',
            'bxgy_badge_buy_get_discount' => 'Buy {buy_qty} Get {get_qty} at {discount}',
            'bxgy_badge_buy_get_shipping' => 'Buy {buy_qty} Get Free Shipping',

            'fixed_heading' => "What's Included:",
            'fixed_button_text' => 'Buy This Bundle',
            'fixed_qty_label' => 'Qty:',
            'fixed_hover_bg' => '#ECFDF5',
            'fixed_layout' => 'list',
            'fixed_selection_text' => 'Choose variant for {product_name}',
            'fixed_product_bg' => '#F9FAFB',
            'fixed_product_title_color' => '#111827',
            'fixed_product_text_color' => '#4B5563',
            'fixed_button_bg' => '#2563EB',
            'fixed_button_color' => '#FFFFFF',
            'fixed_show_regular_price' => true,
            'fixed_price_label' => 'BUNDLE PRICE',
            'fixed_variation_bg' => '#F5F3FF',
            'fixed_variation_text_color' => '#7C3AED',

            'general_cart_success_message' => '🛒 Bundle added to cart!',
            'general_cart_save_text' => 'You Save',
            'general_adding_text' => 'Adding...',
            'general_no_items_text' => 'This bundle has no items configured yet.',
            'general_bundle_price_label' => 'Bundle Price',
            'general_selected_price_label' => 'Selected Price',
            'general_bundle_placement' => 'woocommerce_after_add_to_cart_form',
        ];
    }

    private static function flatten_customizations($customizations)
    {
        $flat = [];

        if (isset($customizations['volume']) && is_array($customizations['volume'])) {
            if (isset($customizations['volume']['heading'])) {
                $flat['volume_heading'] = $customizations['volume']['heading'];
            }
            if (isset($customizations['volume']['button_select_deal'])) {
                $flat['volume_button_select_deal'] = $customizations['volume']['button_select_deal'];
            }
            if (isset($customizations['volume']['button_choose_variations'])) {
                $flat['volume_button_choose_variations'] = $customizations['volume']['button_choose_variations'];
            }
            if (isset($customizations['volume']['button_claim_deal'])) {
                $flat['volume_button_claim_deal'] = $customizations['volume']['button_claim_deal'];
            }
            if (isset($customizations['volume']['hover_bg'])) {
                $flat['volume_hover_bg'] = $customizations['volume']['hover_bg'];
            }
            if (isset($customizations['volume']['layout'])) {
                $flat['volume_layout'] = $customizations['volume']['layout'];
            }
            if (isset($customizations['volume']['tier_prefix'])) {
                $flat['volume_tier_prefix'] = $customizations['volume']['tier_prefix'];
            }
            if (isset($customizations['volume']['discount_text'])) {
                $flat['volume_discount_text'] = $customizations['volume']['discount_text'];
            }
            if (isset($customizations['volume']['badge_bg'])) {
                $flat['volume_badge_bg'] = $customizations['volume']['badge_bg'];
            }
            if (isset($customizations['volume']['badge_color'])) {
                $flat['volume_badge_color'] = $customizations['volume']['badge_color'];
            }
            if (isset($customizations['volume']['button_bg'])) {
                $flat['volume_button_bg'] = $customizations['volume']['button_bg'];
            }
            if (isset($customizations['volume']['button_color'])) {
                $flat['volume_button_color'] = $customizations['volume']['button_color'];
            }
            if (isset($customizations['volume']['show_regular_price'])) {
                $flat['volume_show_regular_price'] = (bool) $customizations['volume']['show_regular_price'];
            }
            if (isset($customizations['volume']['placeholder_select'])) {
                $flat['volume_placeholder_select'] = $customizations['volume']['placeholder_select'];
            }
            if (isset($customizations['volume']['popular_badge_text'])) {
                $flat['volume_popular_badge_text'] = $customizations['volume']['popular_badge_text'];
            }
            if (isset($customizations['volume']['popular_badge_bg'])) {
                $flat['volume_popular_badge_bg'] = $customizations['volume']['popular_badge_bg'];
            }
            if (isset($customizations['volume']['popular_badge_color'])) {
                $flat['volume_popular_badge_color'] = $customizations['volume']['popular_badge_color'];
            }
        }

        if (isset($customizations['mixmatch']) && is_array($customizations['mixmatch'])) {
            if (isset($customizations['mixmatch']['heading'])) {
                $flat['mixmatch_heading'] = $customizations['mixmatch']['heading'];
            }
            if (isset($customizations['mixmatch']['button_claim_deal'])) {
                $flat['mixmatch_button_claim_deal'] = $customizations['mixmatch']['button_claim_deal'];
            }
            if (isset($customizations['mixmatch']['hover_bg'])) {
                $flat['mixmatch_hover_bg'] = $customizations['mixmatch']['hover_bg'];
            }
            if (isset($customizations['mixmatch']['card_bg'])) {
                $flat['mixmatch_card_bg'] = $customizations['mixmatch']['card_bg'];
            }
            $mm_extra = ['header_color','header_badge_bg','price_color','regular_price_color',
                         'tier_active_border','tier_active_text','tier_discount_color',
                         'product_name_color','product_price_color','qty_btn_bg','qty_btn_add_color',
                         'items_bar_bg','items_bar_text','button_bg','button_color','items_selected_label',
                         'header_badge_build', 'tier_suffix', 'discount_text_suffix'];
            foreach ($mm_extra as $key) {
                if (isset($customizations['mixmatch'][$key])) {
                    $flat['mixmatch_' . $key] = $customizations['mixmatch'][$key];
                }
            }
        }

        if (isset($customizations['fbt']) && is_array($customizations['fbt'])) {
            if (isset($customizations['fbt']['heading'])) {
                $flat['fbt_heading'] = $customizations['fbt']['heading'];
            }
            if (isset($customizations['fbt']['button_add_items'])) {
                $flat['fbt_button_add_items'] = $customizations['fbt']['button_add_items'];
            }
            if (isset($customizations['fbt']['button_added'])) {
                $flat['fbt_button_added'] = $customizations['fbt']['button_added'];
            }
            if (isset($customizations['fbt']['hover_bg'])) {
                $flat['fbt_hover_bg'] = $customizations['fbt']['hover_bg'];
            }
            $fbt_extra = ['card_bg','header_color','header_dot_color','badge_bg','badge_color',
                          'product_list_bg','product_item_bg','product_item_border_active',
                          'product_name_color','product_price_color','total_label','total_label_color',
                          'total_price_color','total_regular_price_color','button_bg','button_color',
                          'save_label'];
            foreach ($fbt_extra as $key) {
                if (isset($customizations['fbt'][$key])) {
                    $flat['fbt_' . $key] = $customizations['fbt'][$key];
                }
            }
        }

        if (isset($customizations['bxgy']) && is_array($customizations['bxgy'])) {
            if (isset($customizations['bxgy']['heading'])) {
                $flat['bxgy_heading'] = $customizations['bxgy']['heading'];
            }
            if (isset($customizations['bxgy']['button_claim_offer'])) {
                $flat['bxgy_button_claim_offer'] = $customizations['bxgy']['button_claim_offer'];
            }
            if (isset($customizations['bxgy']['button_claimed'])) {
                $flat['bxgy_button_claimed'] = $customizations['bxgy']['button_claimed'];
            }
            if (isset($customizations['bxgy']['hover_bg'])) {
                $flat['bxgy_hover_bg'] = $customizations['bxgy']['hover_bg'];
            }
            $bxgy_extra = ['card_bg','header_color','header_badge_bg','header_badge_color',
                           'section_title_color','item_bg','item_border','item_name_color',
                           'item_price_color','free_badge_bg','free_badge_color','free_item_border',
                           'free_item_bg','button_bg','button_color','customer_buys_text',
                           'customer_gets_text','purchased_items_text','free_shipping_text',
                           'badge_buy_get_free','badge_buy_get_discount', 'badge_buy_get_shipping',
                           'free_label', 'free_shipping_label'];
            foreach ($bxgy_extra as $key) {
                if (isset($customizations['bxgy'][$key])) {
                    $flat['bxgy_' . $key] = $customizations['bxgy'][$key];
                }
            }
        }

        if (isset($customizations['fixed']) && is_array($customizations['fixed'])) {
            if (isset($customizations['fixed']['heading'])) {
                $flat['fixed_heading'] = $customizations['fixed']['heading'];
            }
            if (isset($customizations['fixed']['button_text'])) {
                $flat['fixed_button_text'] = $customizations['fixed']['button_text'];
            }
            if (isset($customizations['fixed']['hover_bg'])) {
                $flat['fixed_hover_bg'] = $customizations['fixed']['hover_bg'];
            }
            if (isset($customizations['fixed']['layout'])) {
                $flat['fixed_layout'] = $customizations['fixed']['layout'];
            }
            if (isset($customizations['fixed']['selection_text'])) {
                $flat['fixed_selection_text'] = $customizations['fixed']['selection_text'];
            }
            if (isset($customizations['fixed']['product_bg'])) {
                $flat['fixed_product_bg'] = $customizations['fixed']['product_bg'];
            }
            if (isset($customizations['fixed']['product_title_color'])) {
                $flat['fixed_product_title_color'] = $customizations['fixed']['product_title_color'];
            }
            if (isset($customizations['fixed']['product_text_color'])) {
                $flat['fixed_product_text_color'] = $customizations['fixed']['product_text_color'];
            }
            if (isset($customizations['fixed']['button_bg'])) {
                $flat['fixed_button_bg'] = $customizations['fixed']['button_bg'];
            }
            if (isset($customizations['fixed']['button_color'])) {
                $flat['fixed_button_color'] = $customizations['fixed']['button_color'];
            }
            if (isset($customizations['fixed']['show_regular_price'])) {
                $flat['fixed_show_regular_price'] = (bool) $customizations['fixed']['show_regular_price'];
            }
            if (isset($customizations['fixed']['price_label'])) {
                $flat['fixed_price_label'] = $customizations['fixed']['price_label'];
            }
            if (isset($customizations['fixed']['variation_bg'])) {
                $flat['fixed_variation_bg'] = $customizations['fixed']['variation_bg'];
            }
            if (isset($customizations['fixed']['variation_text_color'])) {
                $flat['fixed_variation_text_color'] = $customizations['fixed']['variation_text_color'];
            }
            if (isset($customizations['fixed']['qty_label'])) {
                $flat['fixed_qty_label'] = $customizations['fixed']['qty_label'];
            }
        }

        if (isset($customizations['general']) && is_array($customizations['general'])) {
            if (isset($customizations['general']['cart_success_message'])) {
                $flat['general_cart_success_message'] = $customizations['general']['cart_success_message'];
            }
            if (isset($customizations['general']['cart_save_text'])) {
                $flat['general_cart_save_text'] = $customizations['general']['cart_save_text'];
            }
            if (isset($customizations['general']['bundle_placement'])) {
                $flat['general_bundle_placement'] = $customizations['general']['bundle_placement'];
            }
            if (isset($customizations['general']['adding_text'])) {
                $flat['general_adding_text'] = $customizations['general']['adding_text'];
            }
            if (isset($customizations['general']['no_items_text'])) {
                $flat['general_no_items_text'] = $customizations['general']['no_items_text'];
            }
            if (isset($customizations['general']['bundle_price_label'])) {
                $flat['general_bundle_price_label'] = $customizations['general']['bundle_price_label'];
            }
            if (isset($customizations['general']['selected_price_label'])) {
                $flat['general_selected_price_label'] = $customizations['general']['selected_price_label'];
            }
        }

        return $flat;
    }
}
