<?php
/**
 * Dashboard Handler Class
 * Manages all dashboard functionality for Turbo Templates
 */

if (!defined('ABSPATH')) exit;

class Trstemp_Dashboard {
    
    public function __construct() {
        add_action('admin_enqueue_scripts', array($this, 'enqueue_dashboard_assets'));
    }
    
    /**
     * Enqueue dashboard assets
     */
    public function enqueue_dashboard_assets($hook) {
        if (strpos($hook, 'turbo-starter-templates') === false) {
            return;
        }
        
        // Only load dashboard assets when not in wizard mode
        if (isset($_GET['ci'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only presence check, no form data processed
            return;
        }
        
        // Enqueue dashboard styles
        wp_enqueue_style(
            'trstemp-dashboard',
            TRSTEMP_URI . 'dashboard/css/dashboard.css',
            array(),
            TRSTEMP_VERSION
        );
        
        // Enqueue dashboard scripts
        wp_enqueue_script(
            'trstemp-dashboard',
            TRSTEMP_URI . 'dashboard/js/dashboard.js',
            array('jquery'),
            TRSTEMP_VERSION,
            true
        );
        
        // Localize script with necessary data
        wp_localize_script('trstemp-dashboard', 'trstemp_dashboard', array(
            'nonce' => wp_create_nonce('trstemp_admin_nonce'),
            'ajax_url' => esc_url(admin_url('admin-ajax.php')),
            'home_url' => esc_url(home_url()),
            'admin_url' => esc_url(admin_url())
        ));
    }
    
    /**
     * Render the complete dashboard
     */
    public function render() {
        ?>
        <div class="turbo-dashboard">
            
            <div class="trstemp-turbo-nav-tabs">
                <a href="#overview" class="trstemp-tab-link active">Overview</a>
                <a href="#templates" class="trstemp-tab-link">Templates</a>
                <a href="#toolkit" class="trstemp-tab-link">Toolkit</a>
                <a href="#settings" class="trstemp-tab-link">Settings</a>
            </div>
            
            <div class="turbo-content">
                <!-- Overview Tab -->
                <div id="overview" class="tab-content active">
                    <?php $this->render_overview_tab(); ?>
                </div>
                
                <!-- Templates Tab -->
                <div id="templates" class="tab-content">
                    <?php $this->render_templates_tab(); ?>
                </div>
                
                <!-- Toolkit Tab -->
                <div id="toolkit" class="tab-content">
                    <?php $this->render_toolkit_tab(); ?>
                </div>
                
                <!-- Settings Tab -->
                <div id="settings" class="tab-content">
                    <?php $this->render_settings_tab(); ?>
                </div>
                
                <!-- Free vs Pro CTA Section -->
                <?php $this->render_free_pro_tab(); ?>
            </div>
        </div>
        <?php
    }
    
    /**
     * Render Overview Tab
     */
    private function render_overview_tab() {
        $current_template = get_option('trstemp_current_template', 'None');
        $template_name = get_option('trstemp_current_template_name', 'No template imported');
        $banner_img = esc_url(TRSTEMP_URI . 'assets/images/PreeBuildTemplate.webp');
        ?>
        <div class="overview-grid">
            <div class="overview-card trstemp-current-site-preview">
                <div class="trstemp-card-header">
                    <h3>🏠︎ Current Site</h3>
                    <a href="<?php echo esc_url(home_url()); ?>" class="trstemp-primary-btn " target="_blank"><?php echo esc_html__('View Site', 'turbo-templates-library-for-elementor'); ?></a> <br>
                    <a href="<?php echo esc_url(admin_url('admin.php?page=turbo-starter-templates&ci=0')); ?>" class="trstemp-secondary-btn"><?php echo esc_html__('Change Template', 'turbo-templates-library-for-elementor'); ?></a>
                </div>
                <div class="trstemp-required-plugin-status">
                   <img src="<?php echo esc_url($banner_img); ?>" alt="<?php echo esc_attr($template_name); ?>" class="trstemp-template-img">
                </div>
             </div>
            
            <div class="overview-card quick-stats">
                <div class="trstemp-card-header">
                    <h3>📊 Quick Stats</h3>
                </div>
                <div>
                    <div class="stat-item">
                        <span class="stat-number"><?php echo esc_html(wp_count_posts('page')->publish); ?></span>
                        <span class="stat-label">Pages</span>
                    </div>
                    <div class="stat-item">
                        <span class="stat-number"><?php echo esc_html(wp_count_posts('post')->publish); ?></span>
                        <span class="stat-label">Posts</span>
                    </div>
                    <div class="stat-item">
                        <span class="stat-number"><?php echo count(get_nav_menu_locations()); ?></span>
                        <span class="stat-label">Menus</span>
                    </div>
                </div>
            </div>
            
            <div class="overview-card system-status">
                <div class="trstemp-card-header">
                    <h3>⚙️ System Status</h3>
                </div>
               
                <div>
                    <div class="status-list">
                        <div class="status-item">
                            <span class="status-label">WordPress Version</span>
                            <span class="status-value"><?php echo esc_html(get_bloginfo('version')); ?></span>
                        </div>
                        <div class="status-item">
                            <span class="status-label">PHP Version</span>
                            <span class="status-value"><?php echo esc_html(phpversion()); ?></span>
                        </div>
                        <div class="status-item">
                            <span class="status-label">Elementor Status</span>
                            <span class="status-value"><?php echo defined('ELEMENTOR_VERSION') ? '✅ Active' : '❌ Not Active'; ?></span>
                        </div>
                        <div class="status-item">
                            <span class="status-label">Active Theme</span>
                            <span class="status-value"><?php echo esc_html( wp_get_theme()->get('Name') ); ?></span>
                        </div>
                    </div>
                </div>

            </div>
        </div>
        <?php
    }
    
    /**
     * Render Templates Tab
     */
    private function render_templates_tab() {
        ?>

        <div class="templates-section">
            <div class="section-header">
                <h2>🎨 Template Library</h2>
                <p>Choose from 200+ professional templates</p>
            </div>
            
            <div class="template-actions">
                <a href="<?php echo esc_url(admin_url('admin.php?page=turbo-starter-templates&ci=0')); ?>" class="btn btn-primary btn-large">
                    🚀 Start Building Site
                </a>
                <a href="<?php echo esc_url('https://turbo-addons.com/templates/'); ?>" target='__blank' class="btn btn-secondary btn-large">
                    📚 Browse Templates
                </a>
            </div>
            
            <div class="template-categories">
                <div class="category-card">
                    <div class="category-icon">🏢</div>
                    <h4>Business</h4>
                    <p>Professional business websites</p>
                    <span class="template-count">50+ Templates</span>
                </div>
                <div class="category-card">
                    <div class="category-icon">🛍️</div>
                    <h4>E-commerce</h4>
                    <p>Online stores and shops</p>
                    <span class="template-count">30+ Templates</span>
                </div>
                <div class="category-card">
                    <div class="category-icon">📝</div>
                    <h4>Blog</h4>
                    <p>Personal and professional blogs</p>
                    <span class="template-count">25+ Templates</span>
                </div>
                <div class="category-card">
                    <div class="category-icon">🎨</div>
                    <h4>Portfolio</h4>
                    <p>Creative portfolios</p>
                    <span class="template-count">20+ Templates</span>
                </div>
            </div>
        </div>
        <?php
    }
    
    /**
     * Render Toolkit Tab
     */
    private function render_toolkit_tab() {
        ?>
        <div class="toolkit-section">
            <div class="section-header">
                <h2>🛠️ Essential Toolkit</h2>
                <p>Powerful tools to enhance your WordPress experience</p>
            </div>
            
            <div class="toolkit-grid">
                <div class="tool-card">
                    <div class="tool-icon">🚀</div>
                    <h4>Performance Optimizer</h4>
                    <p>Speed up your website with advanced caching and optimization</p>
                    <div class="tool-status">
                        <span class="status-badge active">Free Feature</span>
                    </div>
                </div>
                
                <div class="tool-card">
                    <div class="tool-icon">🔒</div>
                    <h4>Security Scanner</h4>
                    <p>Protect your site from malware and security threats</p>
                    <div class="tool-status">
                        <span class="status-badge active">Free Feature</span>
                    </div>
                </div>
                
                <div class="tool-card">
                    <div class="tool-icon">☰</div>
                    <h4>Automatic Menu Creation</h4>
                    <p>Menus are automatically created from imported pages, saving setup time.</p>
                    <div class="tool-status">
                        <span class="status-badge active">Free Feature</span>
                    </div>
                </div>
                
                <div class="tool-card">
                    <div class="tool-icon">🎯</div>
                    <h4>SEO Optimizer</h4>
                    <p>Improve your search engine rankings</p>
                    <div class="tool-status">
                        <span class="status-badge active">Free Feature</span>
                    </div>
                </div>
            </div>
        </div>
        
        <?php
    }
    
    /**
     * Render Settings Tab
     */
    private function render_settings_tab() {
        ?>
        <div class="settings-section">
            <div class="section-header">
                <h2>⚙️ Settings</h2>
                <p>Configure your Turbo experience</p>
            </div>
            
            <div class="settings-grid">
                <div class="trstemp-settings-card">
                    <h4>🎨 Customization</h4>
                    <div class="setting-item">
                        <a href="<?php echo esc_url(admin_url('customize.php')); ?>" class="setting-link">
                            Customize Theme
                        </a>
                    </div>
                    <div class="setting-item">
                        <a href="<?php echo esc_url(admin_url('nav-menus.php')); ?>" class="setting-link">
                            Manage Menus
                        </a>
                    </div>
                </div>
                
                <div class="trstemp-settings-card">
                    <h4>📄 Content</h4>
                    <div class="setting-item">
                        <a href="<?php echo esc_url(admin_url('edit.php?post_type=page')); ?>" class="setting-link">
                            Edit Pages
                        </a>
                    </div>
                    <div class="setting-item">
                        <a href="<?php echo esc_url(admin_url('edit.php')); ?>" class="setting-link">
                            Edit Posts
                        </a>
                    </div>
                </div>
                
                <div class="trstemp-settings-card">
                    <h4>🔌 Theme/Plugins</h4>
                    <div class="setting-item">
                        <a href="<?php echo esc_url(admin_url('plugins.php')); ?>" class="setting-link">
                            Manage Plugins
                        </a>
                    </div>
                    <div class="setting-item">
                        <a href="<?php echo esc_url(admin_url('themes.php')); ?>" class="test-import-btn">
                            Manage Theme
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Check if Turbo Addons PRO is active
     */
    private function is_turbo_addons_pro_active() { 
        if ( ! function_exists( 'get_plugins' ) ) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }

        $active_plugins = get_option( 'active_plugins', [] );
        $all_plugins    = get_plugins();

        foreach ( $all_plugins as $plugin_file => $plugin_data ) {
            if (
                in_array( $plugin_file, $active_plugins, true ) &&
                isset( $plugin_data['Name'] ) &&
                $plugin_data['Name'] === 'Turbo Addons Elementor Pro' 
            ) {
                return true;
            }
        }

        return false;
    }

    /**
     * Render Free vs Pro CTA Section
     */
    private function render_free_pro_tab() {
        $is_pro_active = $this->is_turbo_addons_pro_active();
        ?>
            <div class="trstemp-toolkit-cta-container">
                    <!-- Free Card -->
                    <div class="trstemp-free-card trstemp-toolkit-card">
                        <div class="trstemp-card-header">
                            <h3 class="trstemp-card-title">Free Toolkit Features</h3>
                            <div class="trstemp-price-badge trstemp-free-badge">FREE</div>
                        </div>
                        <p class="trstemp-card-description">Get started with essential tools to build your website</p>
                        
                        <ul class="trstemp-features-list">
                            <li class="trstemp-feature-item"><span class="trstemp-check-icon">✓</span> Basic Starter Templates</li>
                            <li class="trstemp-feature-item"><span class="trstemp-check-icon">✓</span> Core Widgets</li>
                            <li class="trstemp-feature-item"><span class="trstemp-check-icon">✓</span> Standard Block Sections</li>
                            <li class="trstemp-feature-item"><span class="trstemp-check-icon">✓</span> Limited Templates Library</li>
                        </ul>
                        
                        <a href="<?php echo esc_url(admin_url('admin.php?page=turbo-starter-templates&ci=0')); ?>" class="trstemp-btn trstemp-btn-secondary trstemp-btn-large">Start Building Free</a>
                    </div>
                    
                    <!-- Premium Card -->
                    <div class="trstemp-premium-card trstemp-toolkit-card trstemp-featured-card">
                        <div class="trstemp-card-header">
                            <h3 class="trstemp-card-title">Premium Toolkit</h3>
                            <div class="trstemp-price-badge trstemp-premium-badge">PRO</div>
                        </div>
                        <p class="trstemp-card-description">
                            <?php echo $is_pro_active 
                                ? 'You have full access to all premium templates, widgets, and resources.'
                                : 'Unlock full power for complete website creation'; ?>
                        </p>
                        
                        <ul class="trstemp-features-list">
                            <li class="trstemp-feature-item"><span class="trstemp-check-icon">✓</span> Complete Website Pages & Design</li>
                            <li class="trstemp-feature-item"><span class="trstemp-check-icon">✓</span> All Premium 200+ Ready-to-Use Templates</li>
                            <li class="trstemp-feature-item"><span class="trstemp-check-icon">✓</span> Turbo Addons with Pro Widgets</li>
                            <li class="trstemp-feature-item"><span class="trstemp-check-icon">✓</span> Ecommerce Pro Widgets</li>
                            <li class="trstemp-feature-item"><span class="trstemp-check-icon">✓</span> All Pro Block Ready Sections</li>
                        </ul>
                        
                        <?php if ($is_pro_active): ?>
                        <a href="<?php echo esc_url(admin_url('admin.php?page=turbo-starter-templates&ci=0')); ?>" class="trstemp-btn trstemp-btn-primary trstemp-btn-large">Start Building Now</a>
                        <?php else: ?>
                            <a href="https://turbo-addons.com/pricing/" class="trstemp-btn trstemp-btn-primary trstemp-btn-large trstemp-upgrade-btn" target="_blank">Upgrade to Premium</a>
                        <?php endif; ?>
                    </div>
                </div>
        <?php
    }
}
