<?php
/**
 * Pawfect Theme Dashboard
 *
 * @package Pawfect
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! class_exists( 'Pawfect_Dashboard' ) ) :

    class Pawfect_Dashboard {

        /**
         * Constructor
         */
        public function __construct() {
            add_action( 'admin_menu', array( $this, 'add_theme_page' ) );
            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
            add_action( 'admin_head', array( $this, 'admin_menu_highlight' ) );
            add_action( 'wp_ajax_pawfect_install_plugins', array( $this, 'ajax_install_plugins' ) );
        }

        /**
         * Register the Dashboard page under Appearance
         */
        public function add_theme_page() {
            add_theme_page(
                esc_html__( 'Pawfect Theme', 'pawfect' ),     // Page title
                esc_html__( 'Pawfect Theme', 'pawfect' ),     // Menu title
                'edit_theme_options',                         // Capability
                'pawfect-dashboard',                          // Menu slug
                array( $this, 'render_dashboard_page' )       // Callback
            );
            
            // Add shortcut to Starter Sites without causing hook deprecation errors
            global $submenu;
            $submenu['themes.php'][] = array( 
                esc_html__( 'Starter Sites', 'pawfect' ), 
                'edit_theme_options', 
                'themes.php?page=pawfect-dashboard&tab=starter-sites' 
            );
        }

        /**
         * Highlight the Pawfect Theme menu item in the admin menu
         */
        public function admin_menu_highlight() {
            ?>
            <style>
                #adminmenu a[href="themes.php?page=pawfect-dashboard"] {
                    color: #fff !important;
                    font-weight: 600 !important;
                    background-color: #728c69 !important;
                    border-radius: 4px;
                    margin-top: 5px;
                    margin-bottom: 5px;
                    padding-left: 10px;
                    transition: background-color 0.15s ease-in-out;
                }
                #adminmenu a[href="themes.php?page=pawfect-dashboard"]:hover,
                #adminmenu a[href="themes.php?page=pawfect-dashboard"]:focus {
                    background-color: #5c7454 !important;
                }
            </style>
            <?php
        }

        /**
         * Enqueue scoped admin styles for the dashboard
         */
        public function enqueue_styles( $hook ) {
            // Only load on our specific dashboard page
            if ( 'appearance_page_pawfect-dashboard' !== $hook ) {
                return;
            }

            $css = "
            /* Full width reset for the dashboard */
            #wpcontent { padding-left: 0; }
            .pawfect-dashboard-app { background: #f0f0f1; min-height: calc(100vh - 32px); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif; }
            
            /* Top Navigation Bar */
            .pawfect-dashboard-header { background: #fff; border-bottom: 1px solid #c3c4c7; display: flex; align-items: center; justify-content: flex-start; gap: 50px; padding: 0 40px; height: 70px; position: sticky; top: 32px; z-index: 10; }
            .pawfect-dashboard-logo { display: flex; align-items: center; gap: 12px; }
            .pawfect-dashboard-logo h3 { margin: 0; font-size: 22px; font-weight: 800; color: #1d2327; letter-spacing: -0.5px; }
            .pawfect-dashboard-logo h3 span { font-weight: 400; color: #646970; }
            .pawfect-version { background: #f0f0f1; padding: 4px 8px; border-radius: 4px; font-size: 11px; font-weight: 600; color: #50575e; letter-spacing: 0.5px; }
            
            .pawfect-dashboard-nav { display: flex; align-items: center; height: 100%; gap: 30px; }
            .pawfect-dashboard-nav a { display: flex; align-items: center; height: 100%; text-decoration: none; color: #50575e; font-weight: 600; font-size: 15px; border-bottom: 3px solid transparent; transition: all 0.2s; padding: 0 5px; }
            .pawfect-dashboard-nav a:hover { color: #1d2327; }
            .pawfect-dashboard-nav a.active { color: #728c69; border-bottom-color: #728c69; }

            /* Main Content Area */
            .pawfect-dashboard-content { max-width: 1200px; margin: 0 auto; padding: 40px 20px; }
            
            .pawfect-dashboard-grid { display: grid; grid-template-columns: 2.2fr 1fr; gap: 40px; align-items: start; }
            @media (max-width: 1024px) { .pawfect-dashboard-grid { grid-template-columns: 1fr; } }
            
            /* Minimal Cards */
            .pawfect-main-col { background: transparent; }
            .pawfect-sidebar-col { display: flex; flex-direction: column; gap: 20px; }
            .pawfect-sidebar-card { background: #fff; padding: 30px; text-align: left; border-radius: 16px; border: 1px solid #e2e8f0; box-shadow: 0 2px 4px rgba(0,0,0,0.02); }
            .pawfect-sidebar-card h3 { font-size: 1.1em; color: #0f172a; margin: 0 0 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; display: flex; align-items: center; gap: 8px; }
            .pawfect-sidebar-card p { font-size: 0.95em; color: #64748b; margin: 0 0 20px; line-height: 1.6; }

            /* Upgrade Features Grid */
            .pawfect-upgrade-features { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; margin-bottom: 40px; }
            @media (max-width: 768px) { .pawfect-upgrade-features { grid-template-columns: 1fr; } }
            .pawfect-feature-box { background: #fff; border: 1px solid #e2e8f0; padding: 25px 20px; border-radius: 12px; text-align: center; transition: transform 0.2s; box-shadow: 0 2px 4px rgba(0,0,0,0.02); }
            .pawfect-feature-box:hover { transform: translateY(-3px); border-color: #cbd5e1; box-shadow: 0 8px 16px rgba(0,0,0,0.06); }
            .pawfect-feature-icon { width: 40px; height: 40px; background: rgba(114, 140, 105, 0.1); color: #728c69; border-radius: 10px; display: flex; align-items: center; justify-content: center; margin: 0 auto 15px; }
            .pawfect-feature-box h4 { margin: 0 0 10px; font-size: 1.1em; color: #0f172a; font-weight: 700; }
            .pawfect-feature-box p { margin: 0; font-size: 0.9em; color: #64748b; line-height: 1.5; }
            
            /* Minimal Table */
            .pawfect-table-wrap { overflow-x: auto; background: #fff; padding: 40px; border-radius: 16px; border: 1px solid #e2e8f0; box-shadow: 0 2px 4px rgba(0,0,0,0.02); }
            .pawfect-comparison-title { font-size: 1.5em; color: #0f172a; margin: 0 0 25px; font-weight: 800; text-align: left; text-transform: uppercase; letter-spacing: 0.05em; }
            .pawfect-comparison-table { width: 100%; border-collapse: separate; border-spacing: 0; border: 1px solid #e2e8f0; border-radius: 12px; overflow: hidden; }
            .pawfect-comparison-table th, .pawfect-comparison-table td { padding: 18px 20px; text-align: left; border-bottom: 1px solid #e2e8f0; color: #475569; font-size: 15px; }
            .pawfect-comparison-table th { background: #f8fafc; font-weight: 700; font-size: 0.95em; color: #0f172a; text-transform: uppercase; letter-spacing: 0.05em; border-bottom: 2px solid #e2e8f0; }
            .pawfect-comparison-table tr:last-child td { border-bottom: none; }
            .pawfect-comparison-table .feature-col { width: 45%; }
            .pawfect-comparison-table .free-col, .pawfect-comparison-table .pro-col { width: 27.5%; text-align: center; }
            .pawfect-comparison-table .pro-col { background: #f3f6f2; }
            .pawfect-comparison-table td.pro-col { font-weight: 600; color: #0f172a; }
            .pawfect-icon-yes { color: #728c69; font-weight: bold; font-size: 1.4em; }
            .pawfect-icon-no { color: #cbd5e1; font-weight: bold; font-size: 1.4em; }
            .pawfect-table-subheader { background: #f1f5f9; font-weight: 700 !important; color: #334155; font-size: 0.85em; text-transform: uppercase; letter-spacing: 0.05em; padding-top: 16px !important; padding-bottom: 12px !important; }
            
            /* CTA & Buttons */
            .pawfect-cta-wrap { text-align: center; padding: 40px; background: #fff; border-radius: 12px; border: 1px solid #e2e8f0; margin-top: 40px; box-shadow: 0 2px 4px rgba(0,0,0,0.02); }
            .pawfect-cta-wrap h3 { font-size: 1.6em; color: #0f172a; margin: 0 0 10px; font-weight: 800; }
            .pawfect-cta-wrap p { font-size: 1.1em; color: #475569; margin: 0 0 25px; }
            .pawfect-btn { display: inline-block; padding: 12px 28px; font-size: 0.95em; font-weight: 600; text-decoration: none; transition: all 0.2s; border: none; cursor: pointer; text-transform: uppercase; letter-spacing: 0.05em; border-radius: 6px; }
            .pawfect-btn-primary { background: #728c69; color: #fff; }
            .pawfect-btn-primary:hover { background: #5c7454; color: #fff; transform: translateY(-1px); box-shadow: 0 4px 6px -1px rgba(114, 140, 105, 0.2); }
            .pawfect-btn-secondary { background: #fff; color: #0f172a; margin-right: 15px; border: 1px solid #cbd5e1; }
            .pawfect-btn-secondary:hover { background: #f8fafc; color: #0f172a; border-color: #94a3b8; }
            .pawfect-btn-outline { background: transparent; border: 1px solid #cbd5e1; color: #334155; padding: 10px 20px; font-size: 0.85em; width: 100%; text-align: center; box-sizing: border-box; }
            .pawfect-btn-outline:hover { background: #f8fafc; color: #728c69; border-color: #728c69; }

            /* Starter Sites Gallery */
            .pawfect-starter-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 24px; }
            @media (max-width: 768px) { .pawfect-starter-grid { grid-template-columns: 1fr; } }
            .pawfect-starter-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 12px; overflow: hidden; transition: transform 0.2s, box-shadow 0.2s; box-shadow: 0 2px 4px rgba(0,0,0,0.02); }
            .pawfect-starter-card:hover { transform: translateY(-3px); box-shadow: 0 8px 20px rgba(0,0,0,0.08); }
            .pawfect-starter-thumb { overflow: hidden; aspect-ratio: 4/3; background: #f0f0f1; position: relative; }
            .pawfect-starter-thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top; display: block; transition: transform 0.4s ease; }
            .pawfect-starter-card:hover .pawfect-starter-thumb img { transform: scale(1.03); }
            .pawfect-starter-info { padding: 20px; }
            .pawfect-starter-info h4 { margin: 0 0 16px; font-size: 1.1em; font-weight: 700; color: #0f172a; }
            .pawfect-starter-actions { display: flex; gap: 10px; }
            .pawfect-starter-actions .pawfect-btn { padding: 8px 18px; font-size: 0.85em; margin-right: 0; }
            .pawfect-thumb-placeholder { display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; background: #f0f0f1; color: #c3c4c7; }
            .pawfect-thumb-placeholder .dashicons { font-size: 48px; width: 48px; height: 48px; }

            /* Locked Pro Cards */
            .pawfect-card-locked .pawfect-starter-thumb img,
            .pawfect-card-locked .pawfect-thumb-placeholder { filter: grayscale(1) brightness(0.9); }
            .pawfect-lock-overlay { position: absolute; inset: 0; background: rgba(0,0,0,0.45); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; color: #fff; font-size: 14px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.1em; }
            .pawfect-lock-overlay .dashicons { font-size: 28px; width: 28px; height: 28px; }

            /* Badges */
            .pawfect-free-badge, .pawfect-pro-badge { position: absolute; top: 12px; right: 12px; padding: 4px 10px; border-radius: 4px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; }
            .pawfect-free-badge { background: #728c69; color: #fff; }
            .pawfect-pro-badge { background: #4b5563; color: #fff; }

            /* Plugin Status */
            .plugin-status { display: inline-flex; align-items: center; gap: 8px; font-size: 0.85em; font-weight: 600; padding: 4px 12px; border-radius: 20px; margin-right: 10px; margin-top: 10px;}
            .plugin-active { background: #ecfdf5; color: #059669; border: 1px solid #10b981; }
            .plugin-inactive { background: #fef2f2; color: #dc2626; border: 1px solid #ef4444; }
            ";
            
            wp_register_style( 'pawfect-dashboard-style', false );
            wp_enqueue_style( 'pawfect-dashboard-style' );
            wp_add_inline_style( 'pawfect-dashboard-style', $css );
        }

        /**
         * Render the dashboard page content
         */
        public function render_dashboard_page() {
            $theme_version = wp_get_theme()->get('Version');
            $active_tab = isset( $_GET['tab'] ) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : 'welcome';
            $pro_active = apply_filters( 'pawfect_is_pro_active', false );
            ?>
            <div class="pawfect-dashboard-app">
                <div class="pawfect-dashboard-header">
                    <div class="pawfect-dashboard-logo">
                        <h3>Pawfect <span>Theme</span></h3>
                        <span class="pawfect-version">v<?php echo esc_html( $theme_version ); ?></span>
                        <?php if ( $pro_active ) : ?>
                            <span style="background:#728c69; color:#fff; padding:3px 10px; border-radius:20px; font-size:11px; font-weight:700; letter-spacing:0.05em; text-transform:uppercase;"><?php esc_html_e( 'Pro Active', 'pawfect' ); ?></span>
                        <?php else : ?>
                            <a href="https://wpmatcha.com/wordpress-themes/pawfect/" target="_blank" style="background:#f3f6f2; color:#728c69; border:1px solid #c8d6c3; padding:3px 10px; border-radius:20px; font-size:11px; font-weight:700; letter-spacing:0.05em; text-transform:uppercase; text-decoration:none;"><?php esc_html_e( 'Get Pro', 'pawfect' ); ?></a>
                        <?php endif; ?>
                    </div>
                    <div class="pawfect-dashboard-nav">
                        <a href="?page=pawfect-dashboard&tab=welcome" class="<?php echo 'welcome' === $active_tab ? 'active' : ''; ?>"><?php esc_html_e( 'Welcome', 'pawfect' ); ?></a>
                        <a href="?page=pawfect-dashboard&tab=starter-sites" class="<?php echo 'starter-sites' === $active_tab ? 'active' : ''; ?>"><?php esc_html_e( 'Starter Sites', 'pawfect' ); ?></a>
                    </div>
                </div>

                <div class="pawfect-dashboard-content">
                    <?php if ( 'starter-sites' === $active_tab ) : ?>
                        <!-- Starter Sites Tab -->
                        <?php
                        $ocdi_active    = is_plugin_active( 'one-click-demo-import/one-click-demo-import.php' );
                        $extra_active   = is_plugin_active( 'matcha-extra/matcha-extra.php' );
                        $base_ready     = $ocdi_active && $extra_active;

                        // Preview images are stored in the plugin usually, but we fallback
                        $preview_base = get_template_directory_uri() . '/assets/images/demos/';

                        $starter_demos = array(
                            array(
                                'name'        => 'Pawfect Default',
                                'preview_url' => 'https://pawfect.wpmatcha.com/',
                                'image_url'   => $preview_base . 'default.jpg',
                                'is_pro'      => false,
                                'import_index'=> 0,
                            ),
                            array(
                                'name'        => 'Pawfect Pro Shop',
                                'preview_url' => 'https://pawfect.wpmatcha.com/pro-shop/',
                                'image_url'   => $preview_base . 'pro-shop.jpg',
                                'is_pro'      => true,
                                'import_index'=> 1,
                                'is_coming_soon' => true,
                            ),
                        );

                        // Grab real preview images from the OCDI filter
                        $ocdi_demos = apply_filters( 'pt-ocdi/import_files', array() );
                        foreach ( $ocdi_demos as $i => $demo ) {
                            if ( isset( $starter_demos[ $i ] ) && ! empty( $demo['import_preview_image_url'] ) ) {
                                $starter_demos[ $i ]['image_url'] = $demo['import_preview_image_url'];
                            }
                        }
                        ?>

                        <?php if ( $base_ready ) : ?>
                            <!-- Plugins active - show demo gallery -->
                            <div class="pawfect-starter-grid">
                                <?php foreach ( $starter_demos as $demo ) :
                                    $is_locked = $demo['is_pro'] && ! $pro_active;
                                ?>
                                    <div class="pawfect-starter-card<?php echo $is_locked ? ' pawfect-card-locked' : ''; ?>">
                                        <div class="pawfect-starter-thumb">
                                            <?php 
                                            $img_style = ! empty( $demo['is_coming_soon'] ) ? 'filter: blur(5px); opacity: 0.8; transform: scale(1.05);' : '';
                                            if ( ! empty( $demo['image_url'] ) ) : ?>
                                                <img src="<?php echo esc_url( $demo['image_url'] ); ?>" alt="<?php echo esc_attr( $demo['name'] ); ?>" style="<?php echo esc_attr( $img_style ); ?>" />
                                            <?php else : ?>
                                                <div class="pawfect-thumb-placeholder">
                                                    <span class="dashicons dashicons-format-image"></span>
                                                </div>
                                            <?php endif; ?>
                                            <?php if ( $is_locked ) : ?>
                                                <div class="pawfect-lock-overlay">
                                                    <span class="dashicons dashicons-lock"></span>
                                                    <span><?php esc_html_e( 'Pro', 'pawfect' ); ?></span>
                                                </div>
                                            <?php endif; ?>
                                            <?php if ( $demo['is_pro'] && $pro_active ) : ?>
                                                <span class="pawfect-pro-badge"><?php esc_html_e( 'Pro', 'pawfect' ); ?></span>
                                            <?php elseif ( ! $demo['is_pro'] ) : ?>
                                                <span class="pawfect-free-badge"><?php esc_html_e( 'Free', 'pawfect' ); ?></span>
                                            <?php endif; ?>
                                        </div>
                                        <div class="pawfect-starter-info">
                                            <h4><?php echo esc_html( $demo['name'] ); ?></h4>
                                            <div class="pawfect-starter-actions">
                                                <?php if ( ! empty( $demo['is_coming_soon'] ) ) : ?>
                                                    <span class="pawfect-btn pawfect-btn-secondary" style="opacity:0.5;cursor:default;"><?php esc_html_e( 'Coming Soon', 'pawfect' ); ?></span>
                                                <?php else : ?>
                                                    <?php if ( ! empty( $demo['preview_url'] ) ) : ?>
                                                        <a href="<?php echo esc_url( $demo['preview_url'] ); ?>" class="pawfect-btn pawfect-btn-secondary" target="_blank" rel="noopener"><?php esc_html_e( 'Preview', 'pawfect' ); ?></a>
                                                    <?php endif; ?>
                                                    <?php if ( $is_locked ) : ?>
                                                        <a href="https://wpmatcha.com/wordpress-themes/pawfect/" class="pawfect-btn pawfect-btn-primary" target="_blank" rel="noopener"><?php esc_html_e( 'Unlock Pro', 'pawfect' ); ?></a>
                                                    <?php elseif ( isset( $ocdi_demos[ $demo['import_index'] ] ) && ! empty( $ocdi_demos[ $demo['import_index'] ]['local_import_file'] ) ) : ?>
                                                        <a href="<?php echo esc_url( admin_url( 'themes.php?page=one-click-demo-import&step=import&import=' . $demo['import_index'] ) ); ?>" class="pawfect-btn pawfect-btn-primary"><?php esc_html_e( 'Import', 'pawfect' ); ?></a>
                                                    <?php else : ?>
                                                        <span class="pawfect-btn pawfect-btn-secondary" style="opacity:0.5;cursor:default;"><?php esc_html_e( 'Coming Soon', 'pawfect' ); ?></span>
                                                    <?php endif; ?>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php else : ?>
                            <!-- Missing required plugins -->
                            <div class="pawfect-cta-wrap" style="max-width: 600px; margin: 40px auto; text-align: center;">
                                <div style="font-size: 48px; color: #728c69; margin-bottom: 20px;">
                                    <span class="dashicons dashicons-warning" style="font-size: 48px; width: 48px; height: 48px;"></span>
                                </div>
                                <h3><?php esc_html_e( 'Required Plugins Missing', 'pawfect' ); ?></h3>
                                <p><?php esc_html_e( 'To import a starter site, you must have "WooCommerce", "One Click Demo Import" and "Matcha Extra" plugins installed and activated.', 'pawfect' ); ?></p>
                                <button id="pawfect-install-plugins-btn" class="pawfect-btn pawfect-btn-primary"><?php esc_html_e( 'Install & Activate Plugins', 'pawfect' ); ?></button>
                            </div>
                        <?php endif; ?>

                    <?php else : ?>
                        <!-- Welcome Tab -->
                        <div class="pawfect-dashboard-grid">
                            
                            <?php if ( $pro_active ) : ?>
                            <!-- PRO Welcome View -->
                            <div class="pawfect-main-col">
                                <h2 style="font-size: 1.8em; margin: 0 0 20px; font-weight: 700; color: #1f2937;"><?php esc_html_e( 'Quick Getting Started Guide', 'pawfect' ); ?></h2>
                                
                                <?php
                                $is_matcha_active = is_plugin_active( 'matcha-extra/matcha-extra.php' );
                                $is_woo_active    = class_exists( 'WooCommerce' );
                                ?>

                                <div style="display:flex; margin-bottom: 25px; align-items:flex-start; gap: 15px;">
                                    <div style="background:#728c69; color:#fff; width:30px; height:30px; border-radius:50%; display:flex; align-items:center; justify-content:center; font-weight:bold; flex-shrink:0;">1</div>
                                    <div>
                                        <h3 style="margin: 0 0 8px; font-size:1.2em;"><?php esc_html_e( 'Install Required Plugins', 'pawfect' ); ?></h3>
                                        <p style="margin: 0; color:#374151; font-size:1.05em;"><?php esc_html_e( 'Pawfect requires Matcha Extra for core features and WooCommerce for e-commerce.', 'pawfect' ); ?></p>
                                        
                                        <div>
                                            <?php if ( $is_matcha_active ) : ?>
                                                <span class="plugin-status plugin-active">&check; Matcha Extra Active</span>
                                            <?php else : ?>
                                                <span class="plugin-status plugin-inactive">&times; Matcha Extra Missing</span>
                                            <?php endif; ?>

                                            <?php if ( $is_woo_active ) : ?>
                                                <span class="plugin-status plugin-active">&check; WooCommerce Active</span>
                                            <?php else : ?>
                                                <span class="plugin-status plugin-inactive">&times; WooCommerce Missing</span>
                                            <?php endif; ?>
                                        </div>

                                        <?php if ( ! $is_matcha_active || ! $is_woo_active ) : ?>
                                        <a href="<?php echo esc_url( admin_url( 'plugins.php' ) ); ?>" class="pawfect-btn pawfect-btn-outline" style="margin-top: 15px; width: auto; font-size: 0.85em; padding: 6px 16px;"><?php esc_html_e( 'Manage Plugins', 'pawfect' ); ?></a>
                                        <?php endif; ?>
                                    </div>
                                </div>

                                <div style="display:flex; margin-bottom: 25px; align-items:flex-start; gap: 15px;">
                                    <div style="background:#728c69; color:#fff; width:30px; height:30px; border-radius:50%; display:flex; align-items:center; justify-content:center; font-weight:bold; flex-shrink:0;">2</div>
                                    <div>
                                        <h3 style="margin: 0 0 8px; font-size:1.2em;"><?php esc_html_e( 'Import a Starter Site', 'pawfect' ); ?></h3>
                                        <p style="margin: 0; color:#374151; font-size:1.05em;"><?php esc_html_e( 'Head over to the Starter Sites tab to import a beautiful pre-built demo and get a huge head start.', 'pawfect' ); ?></p>
                                    </div>
                                </div>

                                <div style="display:flex; align-items:flex-start; gap: 15px;">
                                    <div style="background:#728c69; color:#fff; width:30px; height:30px; border-radius:50%; display:flex; align-items:center; justify-content:center; font-weight:bold; flex-shrink:0;">3</div>
                                    <div>
                                        <h3 style="margin: 0 0 8px; font-size:1.2em;"><?php esc_html_e( 'Customize Your Theme', 'pawfect' ); ?></h3>
                                        <p style="margin: 0; color:#374151; font-size:1.05em;"><?php esc_html_e( 'Launch the Customizer to configure colors, typography, header styles, and more.', 'pawfect' ); ?></p>
                                        <a href="<?php echo esc_url( admin_url( 'customize.php' ) ); ?>" class="pawfect-btn pawfect-btn-primary" style="margin-top:15px;"><?php esc_html_e( 'Launch Customizer', 'pawfect' ); ?></a>
                                    </div>
                                </div>
                            </div>
                            <?php else : ?>
                            <!-- FREE Welcome View (Comparison Table) -->
                            <div class="pawfect-main-col">

                                <!-- Why Upgrade Grid -->
                                <div class="pawfect-upgrade-features">
                                    <div class="pawfect-feature-box">
                                        <div class="pawfect-feature-icon">
                                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><line x1="3" y1="9" x2="21" y2="9"/><line x1="9" y1="21" x2="9" y2="9"/></svg>
                                        </div>
                                        <h4><?php esc_html_e( 'Pro Layouts', 'pawfect' ); ?></h4>
                                        <p><?php esc_html_e( 'Unlock advanced header styles, sticky headers, and custom typography.', 'pawfect' ); ?></p>
                                    </div>
                                    <div class="pawfect-feature-box">
                                        <div class="pawfect-feature-icon">
                                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="9" cy="21" r="1"/><circle cx="20" cy="21" r="1"/><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"/></svg>
                                        </div>
                                        <h4><?php esc_html_e( 'WooCommerce Sync', 'pawfect' ); ?></h4>
                                        <p><?php esc_html_e( 'Deep integration, AJAX cart, quick view, and off-canvas filtering.', 'pawfect' ); ?></p>
                                    </div>
                                    <div class="pawfect-feature-box">
                                        <div class="pawfect-feature-icon">
                                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
                                        </div>
                                        <h4><?php esc_html_e( 'Priority Support', 'pawfect' ); ?></h4>
                                        <p><?php esc_html_e( 'Get VIP 1-on-1 assistance directly from the Pawfect development team.', 'pawfect' ); ?></p>
                                    </div>
                                    <div class="pawfect-feature-box">
                                        <div class="pawfect-feature-icon">
                                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><circle cx="9" cy="21" r="1"></circle><circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg>
                                        </div>
                                        <h4><?php esc_html_e( 'Floating Cart', 'pawfect' ); ?></h4>
                                        <p><?php esc_html_e( 'Off-canvas floating cart for a seamless and distraction-free shopping experience.', 'pawfect' ); ?></p>
                                    </div>
                                    <div class="pawfect-feature-box">
                                        <div class="pawfect-feature-icon">
                                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><polygon points="22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3"></polygon></svg>
                                        </div>
                                        <h4><?php esc_html_e( 'Advanced Filters', 'pawfect' ); ?></h4>
                                        <p><?php esc_html_e( 'Let customers drill down products instantly with powerful AJAX filtering.', 'pawfect' ); ?></p>
                                    </div>
                                    <div class="pawfect-feature-box">
                                        <div class="pawfect-feature-icon">
                                            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect><line x1="3" y1="9" x2="21" y2="9"></line><line x1="9" y1="21" x2="9" y2="9"></line></svg>
                                        </div>
                                        <h4><?php esc_html_e( 'Pro Demos', 'pawfect' ); ?></h4>
                                        <p><?php esc_html_e( 'Unlock all premium starter sites and 1-click import them instantly.', 'pawfect' ); ?></p>
                                    </div>
                                </div>

                                <div class="pawfect-table-wrap">
                                    <h2 class="pawfect-comparison-title"><?php esc_html_e( 'Free vs Pro Comparison', 'pawfect' ); ?></h2>
                                    <table class="pawfect-comparison-table">
                                        <thead>
                                            <tr>
                                                <th class="feature-col"><?php esc_html_e( 'Features', 'pawfect' ); ?></th>
                                                <th class="free-col"><?php esc_html_e( 'Pawfect Free', 'pawfect' ); ?></th>
                                                <th class="pro-col"><?php esc_html_e( 'Pawfect Pro', 'pawfect' ); ?></th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <!-- Group: Theme Core & Layouts -->
                                            <tr>
                                                <td colspan="3" class="pawfect-table-subheader"><?php esc_html_e( 'Theme Core & Layouts', 'pawfect' ); ?></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( 'Core Theme Design & Responsiveness', 'pawfect' ); ?></td>
                                                <td class="free-col"><span class="pawfect-icon-yes">&check;</span></td>
                                                <td class="pro-col"><span class="pawfect-icon-yes">&check;</span></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( '1-Click Demo Import', 'pawfect' ); ?></td>
                                                <td class="free-col"><?php esc_html_e( 'Basic', 'pawfect' ); ?></td>
                                                <td class="pro-col"><strong><?php esc_html_e( 'Pro Demos', 'pawfect' ); ?></strong></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( 'Multiple Header & Footer Layouts', 'pawfect' ); ?></td>
                                                <td class="free-col"><?php esc_html_e( 'Basic', 'pawfect' ); ?></td>
                                                <td class="pro-col"><span class="pawfect-icon-yes">&check;</span></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( 'Sticky Header & Transparent Menus', 'pawfect' ); ?></td>
                                                <td class="free-col"><span class="pawfect-icon-no">&times;</span></td>
                                                <td class="pro-col"><span class="pawfect-icon-yes">&check;</span></td>
                                            </tr>

                                            <!-- Group: Customization & E-Commerce -->
                                            <tr>
                                                <td colspan="3" class="pawfect-table-subheader"><?php esc_html_e( 'Customization & WooCommerce', 'pawfect' ); ?></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( 'Advanced Color & Typography Controls', 'pawfect' ); ?></td>
                                                <td class="free-col"><?php esc_html_e( 'Basic', 'pawfect' ); ?></td>
                                                <td class="pro-col"><span class="pawfect-icon-yes">&check;</span></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( 'WooCommerce Deep Integration', 'pawfect' ); ?></td>
                                                <td class="free-col"><span class="pawfect-icon-yes">&check;</span></td>
                                                <td class="pro-col"><span class="pawfect-icon-yes">&check;</span></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( 'Off-Canvas Floating Cart', 'pawfect' ); ?></td>
                                                <td class="free-col"><span class="pawfect-icon-no">&times;</span></td>
                                                <td class="pro-col"><span class="pawfect-icon-yes">&check;</span></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( 'Product Quick View & Advanced Filters', 'pawfect' ); ?></td>
                                                <td class="free-col"><span class="pawfect-icon-no">&times;</span></td>
                                                <td class="pro-col"><span class="pawfect-icon-yes">&check;</span></td>
                                            </tr>

                                            <!-- Group: Support & Add-ons -->
                                            <tr>
                                                <td colspan="3" class="pawfect-table-subheader"><?php esc_html_e( 'Support & Licensing', 'pawfect' ); ?></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( 'Remove Promo / Author Footer Links', 'pawfect' ); ?></td>
                                                <td class="free-col"><span class="pawfect-icon-no">&times;</span></td>
                                                <td class="pro-col"><span class="pawfect-icon-yes">&check;</span></td>
                                            </tr>
                                            <tr>
                                                <td><?php esc_html_e( 'VIP Priority Support & Updates', 'pawfect' ); ?></td>
                                                <td class="free-col"><?php esc_html_e( 'Forum Only', 'pawfect' ); ?></td>
                                                <td class="pro-col"><strong><?php esc_html_e( '1-on-1 Support', 'pawfect' ); ?></strong></td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>

                                <?php if ( ! $pro_active ) : ?>
                                <div class="pawfect-cta-wrap">
                                    <h3><?php esc_html_e( 'Ready to unlock everything?', 'pawfect' ); ?></h3>
                                    <p><?php esc_html_e( 'Upgrade to Pawfect Pro and get all advanced layouts, WooCommerce sync, and premium support.', 'pawfect' ); ?></p>
                                    <a href="<?php echo esc_url( admin_url( 'customize.php' ) ); ?>" class="pawfect-btn pawfect-btn-secondary"><?php esc_html_e( 'Go to Customizer', 'pawfect' ); ?></a>
                                    <a href="https://wpmatcha.com/wordpress-themes/pawfect/" target="_blank" class="pawfect-btn pawfect-btn-primary"><?php esc_html_e( 'Upgrade to Pro Today', 'pawfect' ); ?></a>
                                </div>
                                <?php endif; ?>
                            </div>
                            <?php endif; // End PRO/FREE Welcome View check ?>

                            <!-- Right Column: Sidebar Networking -->
                            <div class="pawfect-sidebar-col">
                                
                                <!-- Rating -->
                                <div class="pawfect-sidebar-card">
                                    <h3><?php esc_html_e( 'Leave a Review', 'pawfect' ); ?></h3>
                                    <p><?php esc_html_e( 'Are you enjoying the Pawfect theme? We would love it if you could take a minute to leave us a 5-star rating on WordPress.org to help us grow!', 'pawfect' ); ?></p>
                                    <a href="https://wordpress.org/support/theme/pawfect/reviews/#new-post" target="_blank" class="pawfect-btn pawfect-btn-outline"><?php esc_html_e( 'Rate Pawfect', 'pawfect' ); ?></a>
                                </div>

                                <!-- Docs -->
                                <div class="pawfect-sidebar-card">
                                    <h3><?php esc_html_e( 'Documentation', 'pawfect' ); ?></h3>
                                    <p><?php esc_html_e( 'Need help setting up your homepage, importing demos, or changing colors? Check out our extensive theme documentation.', 'pawfect' ); ?></p>
                                    <a href="https://wpmatcha.com/wordpress-themes/pawfect/" target="_blank" class="pawfect-btn pawfect-btn-outline"><?php esc_html_e( 'Read Documentation', 'pawfect' ); ?></a>
                                </div>

                                <!-- Support -->
                                <div class="pawfect-sidebar-card">
                                    <h3><?php esc_html_e( 'Need Priority Support?', 'pawfect' ); ?></h3>
                                    <p><?php esc_html_e( 'Stuck on something? Pawfect Pro users receive instant, 1-on-1 priority support direct from our core development team.', 'pawfect' ); ?></p>
                                    <a href="https://wpmatcha.com/contact/" target="_blank" class="pawfect-btn pawfect-btn-outline"><?php esc_html_e( 'Get Pro Support', 'pawfect' ); ?></a>
                                </div>

                            </div>

                        </div>
                    <?php endif; ?>
                </div>
            </div>
            <?php
        }

        /**
         * AJAX handler to install and activate required plugins
         */
        public function ajax_install_plugins() {
            check_ajax_referer( 'pawfect_admin_nonce', 'nonce' );

            if ( ! current_user_can( 'install_plugins' ) ) {
                wp_send_json_error( array( 'message' => esc_html__( 'Insufficient permissions', 'pawfect' ) ) );
            }

            include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
            include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
            include_once ABSPATH . 'wp-admin/includes/plugin.php';

            $plugins = array(
                'woocommerce'           => 'woocommerce/woocommerce.php',
                'one-click-demo-import' => 'one-click-demo-import/one-click-demo-import.php',
                'matcha-extra'          => 'matcha-extra/matcha-extra.php',
            );

            foreach ( $plugins as $slug => $path ) {
                // Install if not exists
                if ( ! file_exists( WP_PLUGIN_DIR . '/' . $path ) ) {
                    $api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
                    
                    if ( ! is_wp_error( $api ) ) {
                        $skin     = new WP_Ajax_Upgrader_Skin();
                        $upgrader = new Plugin_Upgrader( $skin );
                        $upgrader->install( $api->download_link );
                    }
                }
                
                // Activate if exists
                if ( file_exists( WP_PLUGIN_DIR . '/' . $path ) ) {
                    activate_plugin( $path );
                }
            }

            wp_send_json_success();
        }
    }
endif;
