<?php

declare(strict_types=1);

namespace Instinctor\Services;


if (!defined('ABSPATH')) exit;
use Instinctor\Core\Config;
use Instinctor\Core\DB;

/**
 * Template Editor Service
 * Manages page templates and sections for the storefront builder
 */
class TemplateService
{
    /**
     * Available section types with their default configurations
     */
    public static function getSectionTypes(): array
    {
        return [
            'hero' => [
                'label' => __('Hero Banner', 'instinctor'),
                'icon' => 'megaphone',
                'category' => 'header',
                'defaults' => [
                    'layout' => 'center', // center, left, right
                    'title' => __('Welcome to Our Store', 'instinctor'),
                    'subtitle' => __('Discover amazing products at great prices', 'instinctor'),
                    'button_text' => __('Shop Now', 'instinctor'),
                    'button_url' => '/shop',
                    'button_style' => 'primary', // primary, secondary, outline
                    'image' => '',
                    'overlay' => true,
                    'overlay_color' => 'rgba(0,0,0,0.4)',
                    'bg_color' => '#1a1a1a',
                    'text_color' => '#ffffff',
                    'min_height' => 500,
                    'padding_top' => 80,
                    'padding_bottom' => 80,
                ],
            ],
            'products_grid' => [
                'label' => __('Products Grid', 'instinctor'),
                'icon' => 'grid',
                'category' => 'products',
                'defaults' => [
                    'title' => __('Featured Products', 'instinctor'),
                    'subtitle' => '',
                    'source' => 'featured', // featured, recent, sale, category, manual
                    'category_id' => 0,
                    'product_ids' => [],
                    'count' => 8,
                    'columns' => 4,
                    'show_title' => true,
                    'title_align' => 'center',
                    'bg_color' => '#ffffff',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'products_carousel' => [
                'label' => __('Products Carousel', 'instinctor'),
                'icon' => 'slides',
                'category' => 'products',
                'defaults' => [
                    'title' => __('Best Sellers', 'instinctor'),
                    'subtitle' => '',
                    'source' => 'bestseller',
                    'category_id' => 0,
                    'count' => 10,
                    'visible' => 4,
                    'autoplay' => true,
                    'show_arrows' => true,
                    'show_dots' => true,
                    'bg_color' => '#f8f9fa',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'categories' => [
                'label' => __('Categories', 'instinctor'),
                'icon' => 'folder',
                'category' => 'products',
                'defaults' => [
                    'title' => __('Shop by Category', 'instinctor'),
                    'subtitle' => '',
                    'layout' => 'grid', // grid, icons, cards
                    'columns' => 4,
                    'show_count' => true,
                    'category_ids' => [], // empty = all
                    'bg_color' => '#ffffff',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'text_image' => [
                'label' => __('Text + Image', 'instinctor'),
                'icon' => 'columns',
                'category' => 'content',
                'defaults' => [
                    'layout' => 'image_left', // image_left, image_right
                    'title' => __('About Our Store', 'instinctor'),
                    'content' => __('Tell your story here. Share what makes your brand unique and why customers should choose you.', 'instinctor'),
                    'image' => '',
                    'button_text' => __('Learn More', 'instinctor'),
                    'button_url' => '#',
                    'show_button' => true,
                    'bg_color' => '#ffffff',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'text_block' => [
                'label' => __('Text Block', 'instinctor'),
                'icon' => 'text',
                'category' => 'content',
                'defaults' => [
                    'layout' => 'center', // left, center, right
                    'title' => '',
                    'content' => '',
                    'max_width' => 800,
                    'bg_color' => '#ffffff',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'features' => [
                'label' => __('Features / Icons', 'instinctor'),
                'icon' => 'star',
                'category' => 'content',
                'defaults' => [
                    'title' => __('Why Choose Us', 'instinctor'),
                    'columns' => 4,
                    'items' => [
                        ['icon' => 'truck', 'title' => __('Free Shipping', 'instinctor'), 'text' => __('On orders over $50', 'instinctor')],
                        ['icon' => 'shield', 'title' => __('Secure Payment', 'instinctor'), 'text' => __('100% secure checkout', 'instinctor')],
                        ['icon' => 'refresh', 'title' => __('Easy Returns', 'instinctor'), 'text' => __('30-day return policy', 'instinctor')],
                        ['icon' => 'headphones', 'title' => __('24/7 Support', 'instinctor'), 'text' => __('Dedicated support team', 'instinctor')],
                    ],
                    'bg_color' => '#f8f9fa',
                    'text_color' => '#1a1a1a',
                    'icon_color' => '#22863a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'marketing_quotes' => [
                'label' => __('Marketing Quotes', 'instinctor'),
                'icon' => 'quote',
                'category' => 'content',
                'defaults' => [
                    'title' => __('What People Are Saying', 'instinctor'),
                    'layout' => 'carousel', // carousel, grid
                    'items' => [
                        ['quote' => __('Amazing products and fast shipping!', 'instinctor'), 'name' => 'John D.', 'title' => 'Customer'],
                        ['quote' => __('Best online shopping experience ever.', 'instinctor'), 'name' => 'Sarah M.', 'title' => 'Customer'],
                        ['quote' => __('Great quality and excellent customer service.', 'instinctor'), 'name' => 'Mike R.', 'title' => 'Customer'],
                    ],
                    'show_rating' => false,
                    'disclaimer' => __('These quotes are for marketing purposes.', 'instinctor'),
                    'show_disclaimer' => false,
                    'bg_color' => '#ffffff',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'google_reviews' => [
                'label' => __('Google Reviews', 'instinctor'),
                'icon' => 'star',
                'category' => 'social',
                'defaults' => [
                    'title' => __('Customer Reviews', 'instinctor'),
                    'place_id' => '', // Google Place ID
                    'layout' => 'badge', // badge, carousel, grid
                    'theme' => 'light', // light, dark
                    'min_rating' => 4, // Only show reviews with this rating or higher
                    'max_reviews' => 5,
                    'show_write_review' => true,
                    'bg_color' => '#ffffff',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'verified_reviews' => [
                'label' => __('Verified Reviews', 'instinctor'),
                'icon' => 'check-circle',
                'category' => 'social',
                'defaults' => [
                    'title' => __('Verified Customer Reviews', 'instinctor'),
                    'subtitle' => __('Real reviews from customers who purchased', 'instinctor'),
                    'source' => 'all', // all, product, category
                    'product_id' => 0,
                    'category_id' => 0,
                    'min_rating' => 4,
                    'count' => 6,
                    'layout' => 'grid', // grid, carousel
                    'columns' => 3,
                    'show_product' => true,
                    'show_date' => true,
                    'show_verified_badge' => true,
                    'bg_color' => '#f8f9fa',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'live_activity' => [
                'label' => __('Live Store Activity', 'instinctor'),
                'icon' => 'activity',
                'category' => 'social',
                'defaults' => [
                    'title' => __('Recent Activity', 'instinctor'),
                    'show_purchases' => true,
                    'show_reviews' => true,
                    'show_signups' => false,
                    'count' => 5,
                    'anonymize_names' => true, // "John D. from Philadelphia" vs full name
                    'anonymize_location' => 'city', // city, state, country, none
                    'time_format' => 'relative', // relative (5 min ago) or absolute
                    'max_age_hours' => 72, // Only show activity from last X hours
                    'layout' => 'feed', // feed, ticker
                    'bg_color' => '#ffffff',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'newsletter' => [
                'label' => __('Newsletter', 'instinctor'),
                'icon' => 'mail',
                'category' => 'forms',
                'defaults' => [
                    'title' => __('Subscribe to Our Newsletter', 'instinctor'),
                    'subtitle' => __('Get the latest updates on new products and upcoming sales', 'instinctor'),
                    'button_text' => __('Subscribe', 'instinctor'),
                    'placeholder' => __('Enter your email', 'instinctor'),
                    'success_message' => __('Thank you for subscribing!', 'instinctor'),
                    'bg_color' => '#1a1a1a',
                    'text_color' => '#ffffff',
                    'button_bg' => '#22863a',
                    'button_text_color' => '#ffffff',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'contact_form' => [
                'label' => __('Contact Form', 'instinctor'),
                'icon' => 'send',
                'category' => 'forms',
                'defaults' => [
                    'title' => __('Get in Touch', 'instinctor'),
                    'subtitle' => __('Have a question? We\'d love to hear from you.', 'instinctor'),
                    'show_name' => true,
                    'show_phone' => false,
                    'show_subject' => true,
                    'button_text' => __('Send Message', 'instinctor'),
                    'success_message' => __('Thank you! We\'ll get back to you soon.', 'instinctor'),
                    'recipient_email' => Config::get('admin_email', ''),
                    'bg_color' => '#ffffff',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'faq' => [
                'label' => __('FAQ Accordion', 'instinctor'),
                'icon' => 'help-circle',
                'category' => 'content',
                'defaults' => [
                    'title' => __('Frequently Asked Questions', 'instinctor'),
                    'items' => [
                        ['question' => __('What payment methods do you accept?', 'instinctor'), 'answer' => __('We accept all major credit cards, PayPal, and more.', 'instinctor')],
                        ['question' => __('How long does shipping take?', 'instinctor'), 'answer' => __('Standard shipping takes 3-5 business days.', 'instinctor')],
                        ['question' => __('What is your return policy?', 'instinctor'), 'answer' => __('We offer a 30-day money-back guarantee on all products.', 'instinctor')],
                    ],
                    'bg_color' => '#ffffff',
                    'text_color' => '#1a1a1a',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'video' => [
                'label' => __('Video', 'instinctor'),
                'icon' => 'play-circle',
                'category' => 'media',
                'defaults' => [
                    'title' => '',
                    'video_url' => '',
                    'video_type' => 'youtube', // youtube, vimeo, self
                    'autoplay' => false,
                    'show_controls' => true,
                    'aspect_ratio' => '16:9',
                    'max_width' => 1000,
                    'bg_color' => '#000000',
                    'padding_top' => 0,
                    'padding_bottom' => 0,
                ],
            ],
            'image_gallery' => [
                'label' => __('Image Gallery', 'instinctor'),
                'icon' => 'image',
                'category' => 'media',
                'defaults' => [
                    'title' => '',
                    'show_title' => false,
                    // Content type
                    'content_type' => 'images', // images, products
                    'images' => [],
                    'product_ids' => '', // comma-separated IDs
                    // Layout
                    'layout' => 'grid', // grid, masonry, carousel
                    'columns' => 4,
                    'gap' => 16,
                    // Image styling
                    'aspect_ratio' => '1:1', // 1:1, 4:3, 3:4, 16:9, auto
                    'image_fit' => 'cover', // cover, contain
                    'border_radius' => 8,
                    // Hover & Click
                    'hover_effect' => 'zoom', // none, zoom, fade, gray
                    'link_to' => 'lightbox', // lightbox, link, none
                    // Carousel settings
                    'autoplay_speed' => 3000, // 0 = off
                    'direction' => 'ltr', // ltr, rtl
                    'pause_hover' => true,
                    'show_arrows' => true,
                    'show_dots' => false,
                    'loop' => true,
                    // Section styling
                    'bg_color' => '#ffffff',
                    'padding_top' => 60,
                    'padding_bottom' => 60,
                ],
            ],
            'banner' => [
                'label' => __('Banner / CTA', 'instinctor'),
                'icon' => 'zap',
                'category' => 'content',
                'defaults' => [
                    'layout' => 'inline', // inline, stacked
                    'title' => __('Limited Time Offer', 'instinctor'),
                    'subtitle' => __('Save 20% on your first order', 'instinctor'),
                    'button_text' => __('Shop Now', 'instinctor'),
                    'button_url' => '/shop',
                    'bg_color' => '#1a1a1a',
                    'text_color' => '#ffffff',
                    'padding_top' => 40,
                    'padding_bottom' => 40,
                ],
            ],
            'spacer' => [
                'label' => __('Spacer', 'instinctor'),
                'icon' => 'minus',
                'category' => 'layout',
                'defaults' => [
                    'height' => 60,
                    'height_tablet' => 40,
                    'height_mobile' => 30,
                    'bg_color' => 'transparent',
                    'show_divider' => false,
                    'divider_color' => '#e0e0e0',
                ],
            ],
            'divider' => [
                'label' => __('Divider', 'instinctor'),
                'icon' => 'minus',
                'category' => 'layout',
                'defaults' => [
                    'style' => 'solid', // solid, dashed, dotted
                    'width' => '100%',
                    'thickness' => 1,
                    'color' => '#e0e0e0',
                    'margin_top' => 30,
                    'margin_bottom' => 30,
                ],
            ],
            'html' => [
                'label' => __('Custom HTML', 'instinctor'),
                'icon' => 'code',
                'category' => 'advanced',
                'defaults' => [
                    'html' => '',
                    'css' => '',
                    'padding_top' => 0,
                    'padding_bottom' => 0,
                ],
            ],
        ];
    }

    /**
     * Get SVG icon path for Feather icons (used by Impulse)
     */
    public static function getElementIcon(string $icon): string
    {
        $icons = [
            // Layout - Lucide icons
            'scan' => '<path d="M3 7V5a2 2 0 0 1 2-2h2"/><path d="M17 3h2a2 2 0 0 1 2 2v2"/><path d="M21 17v2a2 2 0 0 1-2 2h-2"/><path d="M7 21H5a2 2 0 0 1-2-2v-2"/>',
            'maximize' => '<path d="M8 3H5a2 2 0 0 0-2 2v3"/><path d="M21 8V5a2 2 0 0 0-2-2h-3"/><path d="M3 16v3a2 2 0 0 0 2 2h3"/><path d="M16 21h3a2 2 0 0 0 2-2v-3"/>',
            'columns' => '<path d="M12 3h7a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-7m0-18H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h7m0-18v18"/>',
            'grid' => '<rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/>',
            'table-cells-merge' => '<path d="M12 21v-6"/><path d="M12 9V3"/><path d="M3 15h18"/><path d="M3 9h18"/><rect width="18" height="18" x="3" y="3" rx="2"/>',
            'table-cells-merge-90' => '<g transform="rotate(90 12 12)"><path d="M12 21v-6"/><path d="M12 9V3"/><path d="M3 15h18"/><path d="M3 9h18"/><rect width="18" height="18" x="3" y="3" rx="2"/></g>',
            'folder' => '<path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/>',
            'gallery-vertical' => '<path d="M3 2h18"/><rect width="18" height="12" x="3" y="6" rx="2"/><path d="M3 22h18"/>',
            'toggle-right' => '<rect width="20" height="12" x="2" y="6" rx="6" ry="6"/><circle cx="16" cy="12" r="2"/>',
            
            // Basic - Lucide icons
            'heading' => '<path d="M6 12h12"/><path d="M6 20V4"/><path d="M18 20V4"/>',
            'align-left' => '<line x1="21" x2="3" y1="6" y2="6"/><line x1="15" x2="3" y1="12" y2="12"/><line x1="17" x2="3" y1="18" y2="18"/>',
            'image' => '<rect width="18" height="18" x="3" y="3" rx="2" ry="2"/><circle cx="9" cy="9" r="2"/><path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21"/>',
            'play-circle' => '<circle cx="12" cy="12" r="10"/><polygon points="10 8 16 12 10 16 10 8"/>',
            'pointer' => '<path d="M22 14a8 8 0 0 1-8 8"/><path d="M18 11v-1a2 2 0 0 0-2-2a2 2 0 0 0-2 2"/><path d="M14 10V9a2 2 0 0 0-2-2a2 2 0 0 0-2 2v1"/><path d="M10 9.5V4a2 2 0 0 0-2-2a2 2 0 0 0-2 2v10"/><path d="M18 11a2 2 0 1 1 4 0v3a8 8 0 0 1-8 8h-2c-2.8 0-4.5-.86-5.99-2.34l-3.6-3.6a2 2 0 0 1 2.83-2.82L7 15"/>',
            'circle-star' => '<circle cx="12" cy="12" r="10"/><polygon points="12 7 13.5 10.5 17.5 11 14.5 13.5 15.5 17 12 15 8.5 17 9.5 13.5 6.5 11 10.5 10.5"/>',
            // Lucide `chart-pie` — used by the Chart block. Paths copied from
            // lucide-icons.php to keep both dictionaries in sync.
            'chart-pie' => '<path d="M21 12c.552 0 1.005-.449.95-.998a10 10 0 0 0-8.953-8.951c-.55-.055-.998.398-.998.95v8a1 1 0 0 0 1 1z"/><path d="M21.21 15.89A10 10 0 1 1 8 2.83"/>',
            // Lucide `table` — used by the Table block (internal type table).
            'table' => '<path d="M12 3v18"/><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M3 9h18"/><path d="M3 15h18"/>',
            'instagram' => '<rect width="20" height="20" x="2" y="2" rx="5" ry="5"/><path d="M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z"/><line x1="17.5" x2="17.51" y1="6.5" y2="6.5"/>',
            'star' => '<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"/>',
            // Lucide hand-platter — used by the Instagent block in the palette.
            'hand-platter' => '<path d="M12 3V2"/><path d="m15.4 17.4 3.2-2.8a2 2 0 1 1 2.8 2.9l-3.6 3.3c-.7.8-1.7 1.2-2.8 1.2h-4c-1.1 0-2.1-.4-2.8-1.2l-1.302-1.464A1 1 0 0 0 6.151 19H5"/><path d="M2 14h12a2 2 0 0 1 0 4h-2"/><path d="M4 10h16"/><path d="M5 10a7 7 0 0 1 14 0"/><path d="M5 14v6a1 1 0 0 1-1 1H2"/>',
            'aperture' => '<circle cx="12" cy="12" r="10"/><path d="m14.31 8 5.74 9.94"/><path d="M9.69 8h11.48"/><path d="m7.38 12 5.74-9.94"/><path d="M9.69 16 3.95 6.06"/><path d="M14.31 16H2.83"/><path d="m16.62 12-5.74 9.94"/>',
            'sliders-horizontal' => '<line x1="21" x2="14" y1="4" y2="4"/><line x1="10" x2="3" y1="4" y2="4"/><line x1="21" x2="12" y1="12" y2="12"/><line x1="8" x2="3" y1="12" y2="12"/><line x1="21" x2="16" y1="20" y2="20"/><line x1="12" x2="3" y1="20" y2="20"/><line x1="14" x2="14" y1="2" y2="6"/><line x1="8" x2="8" y1="10" y2="14"/><line x1="16" x2="16" y1="18" y2="22"/>',
            'arrow-up-0-1' => '<path d="m3 8 4-4 4 4"/><path d="M7 4v16"/><rect x="15" y="4" width="4" height="6" ry="2"/><path d="M17 20v-6h-2"/><path d="M15 20h4"/>',
            'arrow-up-narrow-wide' => '<path d="m3 8 4-4 4 4"/><path d="M7 4v16"/><path d="M11 12h4"/><path d="M11 16h7"/><path d="M11 20h10"/>',
            'countdown' => '<circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/>',
            'chevrons-down-up' => '<path d="m7 20 5-5 5 5"/><path d="m7 4 5 5 5-5"/>',
            'git-commit' => '<circle cx="12" cy="12" r="3"/><line x1="3" x2="9" y1="12" y2="12"/><line x1="15" x2="21" y1="12" y2="12"/>',
            'code' => '<polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/>',
            'whole-word' => '<circle cx="7" cy="12" r="3"/><path d="M10 9v6"/><circle cx="17" cy="12" r="3"/><path d="M14 7v8a3 3 0 0 0 6 0"/><path d="M4 21 21 21"/>',
            'terminal' => '<polyline points="4 17 10 11 4 5"/><line x1="12" x2="20" y1="19" y2="19"/>',
            'panel-left' => '<rect width="18" height="18" x="3" y="3" rx="2"/><line x1="9" x2="9" y1="3" y2="21"/>',
            'fast-forward' => '<polygon points="13 19 22 12 13 5 13 19"/><polygon points="2 19 11 12 2 5 2 19"/>',
            'receipt' => '<path d="M4 2v20l2-1 2 1 2-1 2 1 2-1 2 1 2-1 2 1V2l-2 1-2-1-2 1-2-1-2 1-2-1-2 1Z"/><path d="M16 8h-6a2 2 0 1 0 0 4h4a2 2 0 1 1 0 4H8"/><path d="M12 17.5v-11"/>',
            'brackets' => '<path d="M16 3h3v18h-3"/><path d="M8 21H5V3h3"/>',
            'shapes' => '<path d="M8.3 10a.7.7 0 0 1-.626-1.079L11.4 3a.7.7 0 0 1 1.198-.043L16.3 8.9a.7.7 0 0 1-.572 1.1Z"/><rect x="3" y="14" width="7" height="7" rx="1"/><circle cx="17.5" cy="17.5" r="3.5"/>',
            
            // Media - Lucide icons
            'images' => '<path d="M18 22H4a2 2 0 0 1-2-2V6"/><path d="m22 13-1.296-1.296a2.41 2.41 0 0 0-3.408 0L11 18"/><circle cx="12" cy="8" r="2"/><rect width="16" height="16" x="6" y="2" rx="2"/>',
            'gallery-horizontal' => '<path d="M2 3v18"/><rect width="12" height="18" x="6" y="3" rx="2"/><path d="M22 3v18"/>',
            // 'image-upscale' entry removed 2.1.419 — the SVG path baked in
            // here was wrong (didn't match Lucide's actual image-upscale
            // glyph). The new fallback below pulls from the canonical 1671-
            // icon Lucide registry in includes/lucide-icons.php, so removing
            // this stale entry lets `image-upscale` resolve to the correct
            // glyph automatically. Same applies to any future block icon —
            // no need to mirror it here, just use the Lucide name.
            'map' => '<path d="M14.106 5.553a2 2 0 0 0 1.788 0l3.659-1.83A1 1 0 0 1 21 4.619v12.764a1 1 0 0 1-.553.894l-4.553 2.277a2 2 0 0 1-1.788 0l-4.212-2.106a2 2 0 0 0-1.788 0l-3.659 1.83A1 1 0 0 1 3 19.381V6.618a1 1 0 0 1 .553-.894l4.553-2.277a2 2 0 0 1 1.788 0z"/><path d="M15 5.764v15"/><path d="M9 3.236v15"/>',
            
            // Content - Lucide icons
            'flip-box' => '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="M12 3v18"/><polyline points="8 10 12 6 16 10"/>',
            'megaphone' => '<path d="m3 11 18-5v12L3 13v-2z"/><path d="M11.6 16.8a3 3 0 1 1-5.8-1.6"/>',
            'message-square-warning' => '<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/><path d="M12 7v2"/><path d="M12 13h.01"/>',
            'user-round-check' => '<path d="M2 21a8 8 0 0 1 13.292-6"/><circle cx="10" cy="8" r="5"/><path d="m16 19 2 2 4-4"/>',
            'user-star' => '<path d="M2 21a8 8 0 0 1 10.821-7.487"/><circle cx="10" cy="8" r="5"/><path d="M17 20l.5-1.1c.1-.2.4-.2.5 0l.5 1.1 1.2.2c.2 0 .3.3.1.4l-.9.9.2 1.2c0 .2-.2.4-.4.3l-1.1-.5-1.1.5c-.2.1-.4-.1-.4-.3l.2-1.2-.9-.9c-.2-.1-.1-.4.1-.4l1.2-.2z"/>',
            
            // Store Tools - Lucide icons
            'grip' => '<circle cx="12" cy="5" r="1"/><circle cx="19" cy="5" r="1"/><circle cx="5" cy="5" r="1"/><circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/><circle cx="5" cy="12" r="1"/><circle cx="12" cy="19" r="1"/><circle cx="19" cy="19" r="1"/><circle cx="5" cy="19" r="1"/>',
            'file-spreadsheet' => '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M8 13h2"/><path d="M14 13h2"/><path d="M8 17h2"/><path d="M14 17h2"/>',
            'grip-horizontal' => '<circle cx="12" cy="9" r="1"/><circle cx="19" cy="9" r="1"/><circle cx="5" cy="9" r="1"/><circle cx="12" cy="15" r="1"/><circle cx="19" cy="15" r="1"/><circle cx="5" cy="15" r="1"/>',
            'tag' => '<path d="M12.586 2.586A2 2 0 0 0 11.172 2H4a2 2 0 0 0-2 2v7.172a2 2 0 0 0 .586 1.414l8.704 8.704a2.426 2.426 0 0 0 3.42 0l6.58-6.58a2.426 2.426 0 0 0 0-3.42z"/><circle cx="7.5" cy="7.5" r=".5" fill="currentColor"/>',
            'shopping-cart' => '<circle cx="8" cy="21" r="1"/><circle cx="19" cy="21" r="1"/><path d="M2.05 2.05h2l2.66 12.42a2 2 0 0 0 2 1.58h9.78a2 2 0 0 0 1.95-1.57l1.65-7.43H5.12"/>',
            'shopping-bag' => '<path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4Z"/><path d="M3 6h18"/><path d="M16 10a4 4 0 0 1-8 0"/>',
            'logs' => '<path d="M13 12h8"/><path d="M13 18h8"/><path d="M13 6h8"/><path d="M3 12h1"/><path d="M3 18h1"/><path d="M3 6h1"/><path d="M8 12h1"/><path d="M8 18h1"/><path d="M8 6h1"/>',
            'arrow-big-right-dash' => '<path d="M5 9v6"/><path d="M9 9h3V5l7 7-7 7v-4H9V9z"/>',
            'file-text' => '<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M14 2v4a2 2 0 0 0 2 2h4"/><path d="M10 9H8"/><path d="M16 13H8"/><path d="M16 17H8"/>',
            'percent' => '<line x1="19" x2="5" y1="5" y2="19"/><circle cx="6.5" cy="6.5" r="2.5"/><circle cx="17.5" cy="17.5" r="2.5"/>',
            'truck' => '<path d="M14 18V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v11a1 1 0 0 0 1 1h2"/><path d="M15 18H9"/><path d="M19 18h2a1 1 0 0 0 1-1v-3.65a1 1 0 0 0-.22-.624l-3.48-4.35A1 1 0 0 0 17.52 8H14"/><circle cx="17" cy="18" r="2"/><circle cx="7" cy="18" r="2"/>',
            'circle-user' => '<circle cx="12" cy="12" r="10"/><circle cx="12" cy="10" r="3"/><path d="M7 20.662V19a2 2 0 0 1 2-2h6a2 2 0 0 1 2 2v1.662"/>',
            'circle-check-big' => '<path d="M21.801 10A10 10 0 1 1 17 3.335"/><path d="m9 11 3 3L22 4"/>',
            'square-pen' => '<path d="M12 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.375 2.625a1 1 0 0 1 3 3l-9.013 9.014a2 2 0 0 1-.853.505l-2.873.84a.5.5 0 0 1-.62-.62l.84-2.873a2 2 0 0 1 .506-.852z"/>',
            
            // Navigation - Lucide icons
            'panel-top-dashed' => '<rect width="18" height="18" x="3" y="3" rx="2"/><path d="M14 9h1"/><path d="M19 9h2"/><path d="M3 9h2"/><path d="M9 9h1"/>',
            'panel-bottom-dashed' => '<rect width="18" height="18" x="3" y="3" rx="2"/><path d="M14 15h1"/><path d="M19 15h2"/><path d="M3 15h2"/><path d="M9 15h1"/>',
            'square-menu' => '<rect width="18" height="18" x="3" y="3" rx="2"/><path d="M7 8h10"/><path d="M7 12h10"/><path d="M7 16h10"/>',
            'ellipsis' => '<circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/><circle cx="5" cy="12" r="1"/>',
            'arrow-up-from-dot' => '<path d="m5 9 7-7 7 7"/><path d="M12 16V2"/><circle cx="12" cy="21" r="1"/>',
            'table-properties' => '<path d="M15 3v18"/><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M21 9H3"/><path d="M21 15H3"/>',
            
            // Legacy icons kept for compatibility
            'layout' => '<rect x="3" y="3" width="18" height="18" rx="2"/><line x1="3" y1="9" x2="21" y2="9"/><line x1="9" y1="21" x2="9" y2="9"/>',
            'layers' => '<polygon points="12 2 2 7 12 12 22 7 12 2"/><polyline points="2 17 12 22 22 17"/><polyline points="2 12 12 17 22 12"/>',
            'type' => '<polyline points="4 7 4 4 20 4 20 7"/><line x1="9" x2="15" y1="20" y2="20"/><line x1="12" x2="12" y1="4" y2="20"/>',
            'mouse-pointer' => '<path d="M3 3l7.07 16.97 2.51-7.39 7.39-2.51L3 3z"/><path d="M13 13l6 6"/>',
            'list' => '<line x1="8" x2="21" y1="6" y2="6"/><line x1="8" x2="21" y1="12" y2="12"/><line x1="8" x2="21" y1="18" y2="18"/><line x1="3" x2="3.01" y1="6" y2="6"/><line x1="3" x2="3.01" y1="12" y2="12"/><line x1="3" x2="3.01" y1="18" y2="18"/>',
            'credit-card' => '<rect width="20" height="14" x="2" y="5" rx="2"/><line x1="2" x2="22" y1="10" y2="10"/>',
            'credit-card-flip' => '<rect width="20" height="14" x="2" y="5" rx="2"/><line x1="2" x2="22" y1="14" y2="14"/>',
            'folder-symlink' => '<path d="M2 9.35V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H20a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2v-3a2 2 0 0 1 2-2h7"/><path d="m8 16 3-3-3-3"/>',
            'mail' => '<rect width="20" height="16" x="2" y="4" rx="2"/><path d="m22 7-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7"/>',
            'menu' => '<line x1="4" x2="20" y1="12" y2="12"/><line x1="4" x2="20" y1="6" y2="6"/><line x1="4" x2="20" y1="18" y2="18"/>',
            
            // UI icons
            'chevron-up' => '<path d="m18 15-6-6-6 6"/>',
            'chevron-down' => '<path d="m6 9 6 6 6-6"/>',
            'chevron-left' => '<path d="m15 18-6-6 6-6"/>',
            'chevron-right' => '<path d="m9 18 6-6-6-6"/>',
            'plus' => '<path d="M5 12h14"/><path d="M12 5v14"/>',
            'x' => '<path d="M18 6 6 18"/><path d="m6 6 12 12"/>',
            'trash-2' => '<path d="M3 6h18"/><path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/><path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/><line x1="10" x2="10" y1="11" y2="17"/><line x1="14" x2="14" y1="11" y2="17"/>',
            'copy' => '<rect width="14" height="14" x="8" y="8" rx="2" ry="2"/><path d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2"/>',
            'move' => '<polyline points="5 9 2 12 5 15"/><polyline points="9 5 12 2 15 5"/><polyline points="15 19 12 22 9 19"/><polyline points="19 9 22 12 19 15"/><line x1="2" x2="22" y1="12" y2="12"/><line x1="12" x2="12" y1="2" y2="22"/>',
            'settings' => '<path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"/><circle cx="12" cy="12" r="3"/>',
            'arrow-up' => '<path d="m5 12 7-7 7 7"/><path d="M12 19V5"/>',
            'arrow-down' => '<path d="M12 5v14"/><path d="m19 12-7 7-7-7"/>',
            'edit-2' => '<path d="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/><path d="m15 5 4 4"/>',
            'eye' => '<path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z"/><circle cx="12" cy="12" r="3"/>',
            'search' => '<circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/>',
            'info' => '<circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/>',
            'box' => '<path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z"/><path d="m3.3 7 8.7 5 8.7-5"/><path d="M12 22V12"/>',
            'gift' => '<rect x="3" y="8" width="18" height="4" rx="1"/><path d="M12 8v13"/><path d="M19 12v7a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2v-7"/><path d="M7.5 8a2.5 2.5 0 0 1 0-5A4.8 8 0 0 1 12 8a4.8 8 0 0 1 4.5-5 2.5 2.5 0 0 1 0 5"/>',
            'minus' => '<path d="M5 12h14"/>',
            
            // Store Tools - Additional
            'layout-list' => '<rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/><path d="M14 4h7"/><path d="M14 9h7"/><path d="M14 15h7"/><path d="M14 20h7"/>',
            'layout-list-90' => '<g transform="rotate(90 12 12)"><rect width="7" height="7" x="3" y="3" rx="1"/><rect width="7" height="7" x="3" y="14" rx="1"/><path d="M14 4h7"/><path d="M14 9h7"/><path d="M14 15h7"/><path d="M14 20h7"/></g>',
            'banknote-arrow-up' => '<path d="M12 18H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h16a2 2 0 0 1 2 2v5"/><path d="M18 12h.01"/><path d="M19 22v-6"/><path d="m22 19-3-3-3 3"/><path d="M6 12h.01"/><circle cx="12" cy="12" r="2"/>',
            'log-in' => '<path d="M15 3h4a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2h-4"/><polyline points="10 17 15 12 10 7"/><line x1="15" x2="3" y1="12" y2="12"/>',
            'user-plus' => '<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><line x1="19" x2="19" y1="8" y2="14"/><line x1="22" x2="16" y1="11" y2="11"/>',
            'package-search' => '<path d="M21 10V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l2-1.14"/><path d="m7.5 4.27 9 5.15"/><polyline points="3.29 7 12 12 20.71 7"/><line x1="12" x2="12" y1="22" y2="12"/><circle cx="18.5" cy="15.5" r="2.5"/><path d="M20.27 17.27 22 19"/>',
            
            // Block grid icons
            'app-window' => '<rect x="2" y="4" width="20" height="16" rx="2"/><path d="M10 4v4"/><path d="M2 8h20"/><path d="M6 4v4"/>',
            'app-window-flip' => '<g transform="rotate(180 12 12)"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="M10 4v4"/><path d="M2 8h20"/><path d="M6 4v4"/></g>',
            'archive' => '<rect width="20" height="5" x="2" y="3" rx="1"/><path d="M4 8v11a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8"/><path d="M10 12h4"/>',
            'at-sign' => '<circle cx="12" cy="12" r="4"/><path d="M16 8v5a3 3 0 0 0 6 0v-1a10 10 0 1 0-4 8"/>',
            'award' => '<path d="m15.477 12.89 1.515 8.526a.5.5 0 0 1-.81.47l-3.58-2.687a1 1 0 0 0-1.197 0l-3.586 2.686a.5.5 0 0 1-.81-.469l1.514-8.526"/><circle cx="12" cy="8" r="6"/>',
            'bell' => '<path d="M10.268 21a2 2 0 0 0 3.464 0"/><path d="M3.262 15.326A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326"/>',
            'briefcase' => '<path d="M16 20V4a2 2 0 0 0-2-2h-4a2 2 0 0 0-2 2v16"/><rect width="20" height="14" x="2" y="6" rx="2"/>',
            'calendar' => '<path d="M8 2v4"/><path d="M16 2v4"/><rect width="18" height="18" x="3" y="4" rx="2"/><path d="M3 10h18"/>',
            'calendar-days' => '<path d="M8 2v4"/><path d="M16 2v4"/><rect width="18" height="18" x="3" y="4" rx="2"/><path d="M3 10h18"/><path d="M8 14h.01"/><path d="M12 14h.01"/><path d="M16 14h.01"/><path d="M8 18h.01"/><path d="M12 18h.01"/><path d="M16 18h.01"/>',
            'check-circle' => '<path d="M21.801 10A10 10 0 1 1 17 3.335"/><path d="m9 11 3 3L22 4"/>',
            'circle-play' => '<path d="M9 9.003a1 1 0 0 1 1.517-.859l4.997 2.997a1 1 0 0 1 0 1.718l-4.997 2.997A1 1 0 0 1 9 14.996z"/><circle cx="12" cy="12" r="10"/>',
            'clock' => '<path d="M12 6v6l4 2"/><circle cx="12" cy="12" r="10"/>',
            'cpu' => '<path d="M12 20v2"/><path d="M12 2v2"/><path d="M17 20v2"/><path d="M17 2v2"/><path d="M2 12h2"/><path d="M2 17h2"/><path d="M2 7h2"/><path d="M20 12h2"/><path d="M20 17h2"/><path d="M20 7h2"/><path d="M7 20v2"/><path d="M7 2v2"/><rect x="4" y="4" width="16" height="16" rx="2"/><rect x="8" y="8" width="8" height="8" rx="1"/>',
            'download' => '<path d="M12 15V3"/><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="m7 10 5 5 5-5"/>',
            'edit-3' => '<path d="M13 21h8"/><path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z"/>',
            'globe' => '<circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/>',
            'hash' => '<line x1="4" x2="20" y1="9" y2="9"/><line x1="4" x2="20" y1="15" y2="15"/><line x1="10" x2="8" y1="3" y2="21"/><line x1="16" x2="14" y1="3" y2="21"/>',
            'heart' => '<path d="M2 9.5a5.5 5.5 0 0 1 9.591-3.676.56.56 0 0 0 .818 0A5.49 5.49 0 0 1 22 9.5c0 2.29-1.5 4-3 5.5l-5.492 5.313a2 2 0 0 1-3 .019L5 15c-1.5-1.5-3-3.2-3-5.5"/>',
            'help-circle' => '<circle cx="12" cy="12" r="10"/><path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/><path d="M12 17h.01"/>',
            'home' => '<path d="M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8"/><path d="M3 10a2 2 0 0 1 .709-1.528l7-6a2 2 0 0 1 2.582 0l7 6A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/>',
            'languages' => '<path d="m5 8 6 6"/><path d="m4 14 6-6 2-3"/><path d="M2 5h12"/><path d="M7 2h1"/><path d="m22 22-5-10-5 10"/><path d="M14 18h6"/>',
            'list-check' => '<path d="M16 5H3"/><path d="M16 12H3"/><path d="M11 19H3"/><path d="m15 18 2 2 4-4"/>',
            'map-pin' => '<path d="M20 10c0 4.993-5.539 10.193-7.399 11.799a1 1 0 0 1-1.202 0C9.539 20.193 4 14.993 4 10a8 8 0 0 1 16 0"/><circle cx="12" cy="10" r="3"/>',
            'maximize-2' => '<path d="M15 3h6v6"/><path d="m21 3-7 7"/><path d="m3 21 7-7"/><path d="M9 21H3v-6"/>',
            'message-circle' => '<path d="M2.992 16.342a2 2 0 0 1 .094 1.167l-1.065 3.29a1 1 0 0 0 1.236 1.168l3.413-.998a2 2 0 0 1 1.099.092 10 10 0 1 0-4.777-4.719"/>',
            'monitor' => '<rect width="20" height="14" x="2" y="3" rx="2"/><line x1="8" x2="16" y1="21" y2="21"/><line x1="12" x2="12" y1="17" y2="21"/>',
            'package' => '<path d="M11 21.73a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73z"/><path d="M12 22V12"/><polyline points="3.29 7 12 12 20.71 7"/><path d="m7.5 4.27 9 5.15"/>',
            'picture-in-picture-2' => '<path d="M21 9V6a2 2 0 0 0-2-2H4a2 2 0 0 0-2 2v10c0 1.1.9 2 2 2h4"/><rect width="10" height="7" x="12" y="13" rx="2"/>',
            'plus-circle' => '<circle cx="12" cy="12" r="10"/><path d="M8 12h8"/><path d="M12 8v8"/>',
            'receipt' => '<path d="M4 2v20l2-1 2 1 2-1 2 1 2-1 2 1 2-1 2 1V2l-2 1-2-1-2 1-2-1-2 1-2-1-2 1Z"/><path d="M16 8h-6a2 2 0 1 0 0 4h4a2 2 0 1 1 0 4H8"/><path d="M12 17.5v-11"/>',
            'refresh-cw' => '<path d="M3 12a9 9 0 0 1 9-9 9.75 9.75 0 0 1 6.74 2.74L21 8"/><path d="M21 3v5h-5"/><path d="M21 12a9 9 0 0 1-9 9 9.75 9.75 0 0 1-6.74-2.74L3 16"/><path d="M8 16H3v5"/>',
            'rotate-ccw' => '<path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/>',
            'send' => '<path d="M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z"/><path d="m21.854 2.147-10.94 10.939"/>',
            'share-2' => '<circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><line x1="8.59" x2="15.42" y1="13.51" y2="17.49"/><line x1="15.41" x2="8.59" y1="6.51" y2="10.49"/>',
            'shield' => '<path d="M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z"/>',
            'trending-up' => '<path d="M16 7h6v6"/><path d="m22 7-8.5 8.5-5-5L2 17"/>',
            'users' => '<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><path d="M16 3.128a4 4 0 0 1 0 7.744"/><path d="M22 21v-2a4 4 0 0 0-3-3.87"/><circle cx="9" cy="7" r="4"/>',
            'zap' => '<path d="M4 14a1 1 0 0 1-.78-1.63l9.9-10.2a.5.5 0 0 1 .86.46l-1.92 6.02A1 1 0 0 0 13 10h7a1 1 0 0 1 .78 1.63l-9.9 10.2a.5.5 0 0 1-.86-.46l1.92-6.02A1 1 0 0 0 11 14z"/>',
        ];
        
        // Lookup order:
        //   1. Local dict above — preserves any deliberate overrides
        //      (compound icons, gated visuals, etc.).
        //   2. Canonical Lucide registry (1671 icons, auto-generated).
        //      Lets new block icons be referenced by Lucide name without
        //      mirroring the SVG path here. This is how `image-upscale`
        //      resolves — the entry was removed from the local dict in
        //      2.1.419 because its baked path was incorrect; the fallback
        //      pulls the right glyph from lucide-icons.php.
        //   3. Last resort: star (so an unknown icon still renders).
        if (isset($icons[$icon])) return $icons[$icon];
        if (function_exists('impulse_lucide_icons')) {
            $lucide = impulse_lucide_icons();
            if (isset($lucide[$icon])) return $lucide[$icon];
        }
        return $icons['star'];
    }

    /**
     * Get section categories
     */
    public static function getSectionCategories(): array
    {
        return [
            'header' => __('Header', 'instinctor'),
            'products' => __('Products', 'instinctor'),
            'content' => __('Content', 'instinctor'),
            'social' => __('Social Proof', 'instinctor'),
            'forms' => __('Forms', 'instinctor'),
            'media' => __('Media', 'instinctor'),
            'layout' => __('Layout', 'instinctor'),
            'advanced' => __('Advanced', 'instinctor'),
        ];
    }

    /**
     * Get starter templates
     */
    public static function getStarterTemplates(): array
    {
        return [
            'minimal' => [
                'name' => __('Minimal', 'instinctor'),
                'description' => __('Clean, whitespace-focused design', 'instinctor'),
                'preview' => 'minimal.jpg',
                'sections' => [
                    ['type' => 'products_grid', 'settings' => ['title' => __('New Arrivals', 'instinctor'), 'source' => 'recent', 'count' => 8, 'columns' => 4]],
                    ['type' => 'text_image', 'settings' => ['layout' => 'image_right']],
                ],
            ],
            'modern' => [
                'name' => __('Modern', 'instinctor'),
                'description' => __('Bold design with dark accents', 'instinctor'),
                'preview' => 'modern.jpg',
                'sections' => [
                    ['type' => 'products_carousel', 'settings' => ['title' => __('Trending Now', 'instinctor'), 'source' => 'featured']],
                    ['type' => 'verified_reviews', 'settings' => ['bg_color' => '#f8f9fa']],
                ],
            ],
            'classic' => [
                'name' => __('Classic', 'instinctor'),
                'description' => __('Traditional e-commerce layout', 'instinctor'),
                'preview' => 'classic.jpg',
                'sections' => [
                    ['type' => 'products_grid', 'settings' => ['title' => __('Featured Products', 'instinctor'), 'source' => 'featured', 'count' => 8]],
                    ['type' => 'products_grid', 'settings' => ['title' => __('On Sale', 'instinctor'), 'source' => 'sale', 'count' => 4]],
                ],
            ],
            'boutique' => [
                'name' => __('Boutique', 'instinctor'),
                'description' => __('Elegant, luxury-focused design', 'instinctor'),
                'preview' => 'boutique.jpg',
                'sections' => [
                    ['type' => 'spacer', 'settings' => ['height' => 80]],
                    ['type' => 'text_block', 'settings' => ['layout' => 'center', 'title' => __('Our Story', 'instinctor'), 'content' => __('Crafting exceptional experiences since day one.', 'instinctor')]],
                    ['type' => 'products_grid', 'settings' => ['title' => __('The Collection', 'instinctor'), 'columns' => 3, 'count' => 6]],
                    ['type' => 'image_gallery', 'settings' => ['columns' => 3]],
                    ['type' => 'verified_reviews', 'settings' => ['layout' => 'grid']],
                ],
            ],
        ];
    }

    /**
     * Create database tables
     */
    public static function createTables(): void
    {
        
        $charset_collate = DB::get_charset_collate();
        
        // Templates/Pages table
        $table_templates = DB::prefix() . 'instinctor_templates';
        $sql_templates = "CREATE TABLE $table_templates (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            name varchar(255) NOT NULL,
            slug varchar(100) NOT NULL,
            type varchar(50) NOT NULL DEFAULT 'page',
            status varchar(20) NOT NULL DEFAULT 'draft',
            settings longtext,
            created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY slug (slug),
            KEY type (type),
            KEY status (status)
        ) $charset_collate;";
        
        // Elements table (supports unlimited nesting via parent_id)
        $table_sections = DB::prefix() . 'instinctor_template_sections';
        $sql_sections = "CREATE TABLE $table_sections (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            template_id bigint(20) unsigned NOT NULL,
            parent_id bigint(20) unsigned DEFAULT NULL,
            element_type varchar(100) NOT NULL,
            settings longtext,
            sort_order int(11) NOT NULL DEFAULT 0,
            status varchar(20) NOT NULL DEFAULT 'active',
            created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY template_id (template_id),
            KEY parent_id (parent_id),
            KEY sort_order (sort_order)
        ) $charset_collate;";
        
        // Element templates library
        $table_element_templates = DB::prefix() . 'instinctor_element_templates';
        $sql_element_templates = "CREATE TABLE $table_element_templates (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            name varchar(255) NOT NULL,
            type varchar(50) NOT NULL DEFAULT 'section',
            category varchar(100) DEFAULT NULL,
            thumbnail varchar(500) DEFAULT NULL,
            content longtext NOT NULL,
            is_global tinyint(1) NOT NULL DEFAULT 0,
            created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY type (type),
            KEY category (category)
        ) $charset_collate;";
        
        // In WP mode, dbDelta handles schema diffs gracefully. In standalone, that file
        // doesn't exist — just run CREATE TABLE IF NOT EXISTS directly (DB::get_charset_collate
        // emits the charset clause so the DDL is safe on both MySQL and MariaDB).
        if (defined('INSTINCTOR_ENV') && INSTINCTOR_ENV === 'standalone') {
            DB::query(str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ', $sql_templates));
            DB::query(str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ', $sql_sections));
            DB::query(str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ', $sql_element_templates));
        } else {
            require_once ABSPATH . 'wp-admin/includes/upgrade.php';
            dbDelta($sql_templates);
            dbDelta($sql_sections);
            dbDelta($sql_element_templates);
        }
        
        // Add parent_id column if it doesn't exist (for existing installs)
        $column = DB::get_results("SHOW COLUMNS FROM $table_sections LIKE 'parent_id'");
        if (empty($column)) {
            DB::query("ALTER TABLE $table_sections ADD parent_id bigint(20) unsigned DEFAULT NULL AFTER template_id");
            DB::query("ALTER TABLE $table_sections ADD KEY parent_id (parent_id)");
        }
        
        // Rename section_type to element_type if needed (for existing installs)
        $column = DB::get_results("SHOW COLUMNS FROM $table_sections LIKE 'section_type'");
        if (!empty($column)) {
            DB::query("ALTER TABLE $table_sections CHANGE section_type element_type varchar(100) NOT NULL");
        }
    }

    /**
     * Get template by ID
     */
    public static function getTemplate(int $id): ?object
    {
        $table = DB::prefix() . 'instinctor_templates';
        
        $template = DB::get_row(DB::prepare(
            "SELECT * FROM $table WHERE id = %d",
            $id
        ));
        
        if ($template && $template->settings) {
            $template->settings = json_decode($template->settings, true);
        }
        
        return $template;
    }

    /**
     * Get template by slug
     */
    public static function getTemplateBySlug(string $slug): ?object
    {
        $table = DB::prefix() . 'instinctor_templates';
        
        $template = DB::get_row(DB::prepare(
            "SELECT * FROM $table WHERE slug = %s",
            $slug
        ));
        
        if ($template && $template->settings) {
            $template->settings = json_decode($template->settings, true);
        }
        
        return $template;
    }

    /**
     * Get all templates
     */
    public static function getTemplates(string $type = ''): array
    {
        $table = DB::prefix() . 'instinctor_templates';
        
        $where = '';
        if ($type) {
            $where = DB::prepare(" WHERE type = %s", $type);
        }
        
        $templates = DB::get_results("SELECT * FROM $table $where ORDER BY name ASC");
        
        foreach ($templates as &$template) {
            if ($template->settings) {
                $template->settings = json_decode($template->settings, true);
            }
        }
        
        return $templates;
    }

    /**
     * Save template
     */
    public static function saveTemplate(array $data): int
    {
        $table = DB::prefix() . 'instinctor_templates';
        
        $settings = isset($data['settings']) ? wp_json_encode($data['settings']) : '{}';
        
        if (!empty($data['id'])) {
            DB::update(
                $table,
                [
                    'name' => sanitize_text_field($data['name']),
                    'slug' => sanitize_title($data['slug'] ?? $data['name']),
                    'type' => sanitize_text_field($data['type'] ?? 'page'),
                    'status' => sanitize_text_field($data['status'] ?? 'draft'),
                    'settings' => $settings,
                ],
                ['id' => intval($data['id'])],
                ['%s', '%s', '%s', '%s', '%s'],
                ['%d']
            );
            return intval($data['id']);
        }
        
        DB::insert(
            $table,
            [
                'name' => sanitize_text_field($data['name']),
                'slug' => sanitize_title($data['slug'] ?? $data['name']),
                'type' => sanitize_text_field($data['type'] ?? 'page'),
                'status' => sanitize_text_field($data['status'] ?? 'draft'),
                'settings' => $settings,
            ],
            ['%s', '%s', '%s', '%s', '%s']
        );
        
        return (int) DB::insert_id();
    }

    /**
     * Delete template
     */
    public static function deleteTemplate(int $id): bool
    {
        
        // Delete sections first
        $sections_table = DB::prefix() . 'instinctor_template_sections';
        DB::delete($sections_table, ['template_id' => $id], ['%d']);
        
        // Delete template
        $table = DB::prefix() . 'instinctor_templates';
        return (bool) DB::delete($table, ['id' => $id], ['%d']);
    }

    /**
     * Get template sections
     */
    public static function getTemplateSections(int $templateId): array
    {
        $table = DB::prefix() . 'instinctor_template_sections';
        
        $sections = DB::get_results(DB::prepare(
            "SELECT * FROM $table WHERE template_id = %d AND status = 'active' ORDER BY sort_order ASC",
            $templateId
        ));
        
        $sectionTypes = self::getSectionTypes();
        
        foreach ($sections as &$section) {
            $section->settings = $section->settings ? json_decode($section->settings, true) : [];
            
            // Merge with defaults
            if (isset($sectionTypes[$section->section_type])) {
                $section->settings = array_merge(
                    $sectionTypes[$section->section_type]['defaults'],
                    $section->settings
                );
                $section->label = $sectionTypes[$section->section_type]['label'];
                $section->icon = $sectionTypes[$section->section_type]['icon'];
            }
        }
        
        return $sections;
    }

    /**
     * Save section
     */
    public static function saveSection(array $data): int
    {
        $table = DB::prefix() . 'instinctor_template_sections';
        
        if (!empty($data['id'])) {
            // Update existing - only update provided fields
            $update = [];
            $format = [];
            
            if (isset($data['settings'])) {
                $update['settings'] = wp_json_encode($data['settings']);
                $format[] = '%s';
            }
            if (!empty($data['section_type'])) {
                $update['section_type'] = sanitize_text_field($data['section_type']);
                $format[] = '%s';
            }
            if (isset($data['sort_order'])) {
                $update['sort_order'] = intval($data['sort_order']);
                $format[] = '%d';
            }
            if (!empty($data['status'])) {
                $update['status'] = sanitize_text_field($data['status']);
                $format[] = '%s';
            }
            
            if (!empty($update)) {
                DB::update($table, $update, ['id' => intval($data['id'])], $format, ['%d']);
            }
            return intval($data['id']);
        }
        
        // Insert new
        $settings = isset($data['settings']) ? wp_json_encode($data['settings']) : '{}';
        
        DB::insert(
            $table,
            [
                'template_id' => intval($data['template_id']),
                'section_type' => sanitize_text_field($data['section_type']),
                'settings' => $settings,
                'sort_order' => intval($data['sort_order'] ?? 0),
                'status' => 'active',
            ],
            ['%d', '%s', '%s', '%d', '%s']
        );
        
        return (int) DB::insert_id();
    }

    /**
     * Delete section
     */
    public static function deleteSection(int $id): bool
    {
        $table = DB::prefix() . 'instinctor_template_sections';
        return (bool) DB::delete($table, ['id' => $id], ['%d']);
    }

    /**
     * Reorder sections
     */
    public static function reorderSections(int $templateId, array $sectionIds): bool
    {
        $table = DB::prefix() . 'instinctor_template_sections';
        
        foreach ($sectionIds as $order => $sectionId) {
            DB::update(
                $table,
                ['sort_order' => $order],
                ['id' => intval($sectionId), 'template_id' => $templateId],
                ['%d'],
                ['%d', '%d']
            );
        }
        
        return true;
    }

    /**
     * Duplicate template
     */
    public static function duplicateTemplate(int $id): int
    {
        $template = self::getTemplate($id);
        if (!$template) {
            return 0;
        }
        
        // Create new template
        $newId = self::saveTemplate([
            'name' => $template->name . ' (Copy)',
            'slug' => $template->slug . '-copy-' . time(),
            'type' => $template->type,
            'status' => 'draft',
            'settings' => $template->settings,
        ]);
        
        // Copy sections
        $sections = self::getTemplateSections($id);
        foreach ($sections as $section) {
            self::saveSection([
                'template_id' => $newId,
                'section_type' => $section->section_type,
                'settings' => $section->settings,
                'sort_order' => $section->sort_order,
            ]);
        }
        
        return $newId;
    }

    /**
     * Apply starter template
     */
    public static function applyStarterTemplate(int $templateId, string $starterKey): bool
    {
        $starters = self::getStarterTemplates();
        if (!isset($starters[$starterKey])) {
            return false;
        }
        
        $starter = $starters[$starterKey];
        $sectionTypes = self::getSectionTypes();
        
        // Clear existing sections
        $table = DB::prefix() . 'instinctor_template_sections';
        DB::delete($table, ['template_id' => $templateId], ['%d']);
        
        // Add starter sections
        foreach ($starter['sections'] as $order => $section) {
            $defaults = $sectionTypes[$section['type']]['defaults'] ?? [];
            $settings = array_merge($defaults, $section['settings'] ?? []);
            
            self::saveSection([
                'template_id' => $templateId,
                'section_type' => $section['type'],
                'settings' => $settings,
                'sort_order' => $order,
            ]);
        }
        
        return true;
    }

    /**
     * Export template as JSON
     */
    public static function exportTemplate(int $id): ?string
    {
        $template = self::getTemplate($id);
        if (!$template) {
            return null;
        }
        
        $sections = self::getTemplateSections($id);
        
        $export = [
            'name' => $template->name,
            'type' => $template->type,
            'settings' => $template->settings,
            'sections' => array_map(function($s) {
                return [
                    'type' => $s->section_type,
                    'settings' => $s->settings,
                    'sort_order' => $s->sort_order,
                ];
            }, $sections),
            'exported_at' => current_time('mysql'),
            'version' => INSTINCTOR_VERSION,
        ];
        
        return wp_json_encode($export, JSON_PRETTY_PRINT);
    }

    /**
     * Import template from JSON
     */
    public static function importTemplate(string $json): int
    {
        $data = json_decode($json, true);
        if (!$data || !isset($data['sections'])) {
            return 0;
        }
        
        // Create template
        $templateId = self::saveTemplate([
            'name' => $data['name'] . ' (Imported)',
            'slug' => sanitize_title($data['name']) . '-imported-' . time(),
            'type' => $data['type'] ?? 'page',
            'status' => 'draft',
            'settings' => $data['settings'] ?? [],
        ]);
        
        // Add sections
        foreach ($data['sections'] as $section) {
            self::saveSection([
                'template_id' => $templateId,
                'section_type' => $section['type'],
                'settings' => $section['settings'] ?? [],
                'sort_order' => $section['sort_order'] ?? 0,
            ]);
        }
        
        return $templateId;
    }
}
