<?php
// Main Dashboard Controller
defined( 'ABSPATH' ) || exit;

// Get current options
// Get current options
$cafi_options = get_option( 'cafi_settings' );

// Determine Tab from URL Page Slug
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$cafi_current_page_slug = isset($_GET['page']) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
$cafi_current_tab = 'overview';

if ($cafi_current_page_slug === 'auto-featured-image-settings') {
    $cafi_current_tab = 'settings';
} elseif ($cafi_current_page_slug === 'auto-featured-image-templates') {
    $cafi_current_tab = 'templates';
}
// Fallback/Safety: JS handles actual class switching, but we pass this to JS or set initial class.

// Default Logo Logic
// Only set default if key is missing (fresh install). 
// If it exists but is empty (user removed it), preserve empty state.
if ( ! isset( $cafi_options['logo_url'] ) ) {
    $cafi_options['logo_url'] = plugin_dir_url( __FILE__ ) . 'assets/images/logo.png';
}
?>


<div id="afi-dashboard">
    
    <!-- Pickr Library for Color Picker (Enqueued via settings-page.php) -->
    
    <!-- COL 1: SIDEBAR -->
    <aside class="afi-sidebar">
        <div class="afi-brand">
            <div class="afi-brand-icon" style="background:transparent; box-shadow:none;">
                <img src="<?php echo esc_url( plugin_dir_url(__FILE__) . 'assets/images/logo.png' ); ?>" alt="AutoFeatured" style="width:100%; height:100%; object-fit:contain;">
            </div>
        </div>
        
        <nav class="afi-nav">
            <a class="afi-nav-item active" data-view="overview">
                <span class="material-symbols-outlined">dashboard</span>
                <span>Overview</span>
            </a>
            <a class="afi-nav-item" data-view="settings">
                <span class="material-symbols-outlined">settings</span>
                <span>Settings</span>
            </a>
            <a class="afi-nav-item" data-view="templates">
                <span class="material-symbols-outlined">grid_view</span>
                <span>Templates</span>
            </a>
        </nav>


    </aside>

    <!-- COL 2: MAIN CONTENT -->
    <main class="afi-main">
        <form id="afi-main-form" method="post" action="options.php" style="height: 100%; display: flex; flex-direction: column;">
            <?php settings_fields( 'cafi_plugin' ); ?>
            <!-- Hidden Input for Template Selection -->
            <input type="hidden" id="afi_selected_template" name="cafi_settings[template]" value="<?php echo isset($cafi_options['template']) ? esc_attr($cafi_options['template']) : ''; ?>">

            <!-- VIEW: OVERVIEW -->
            <div id="view-overview" class="afi-view active">
                <?php include plugin_dir_path(__FILE__) . 'views/overview.php'; ?>
            </div>

            <!-- VIEW: SETTINGS -->
            <div id="view-settings" class="afi-view">
                <?php include plugin_dir_path(__FILE__) . 'views/settings.php'; ?>
            </div>

            <!-- VIEW: TEMPLATES -->
            <div id="view-templates" class="afi-view">
                <?php include plugin_dir_path(__FILE__) . 'views/templates.php'; ?>
            </div>
            
            <!-- Floating Save Bar (Visible in Settings/Templates mostly) -->
            <div class="afi-save-bar">
                <button type="button" id="afi-save-settings" class="afi-btn-primary">
                    <svg class="save-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
                        <path d="M19 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h11l5 5v11a2 2 0 0 1-2 2z"/>
                        <polyline points="17 21 17 13 7 13 7 21"/>
                        <polyline points="7 3 7 8 15 8"/>
                    </svg>
                    Save Changes
                </button>
            </div>
        </form>
    </main>

    <!-- COL 3: PREVIEW PANEL -->
    <!-- COL 3: PREVIEW PANEL -->
    <aside class="afi-preview-panel">
        
        <!-- Live Preview Card (Stitch Design) -->
        <div class="afi-preview-card">
            
            <!-- Card Header -->
            <div class="afi-preview-card-header">
                <div class="afi-preview-header-title">
                    <span class="material-symbols-outlined">visibility</span>
                    <span>Live Preview</span>
                </div>
            </div>

            <div class="afi-preview-card-body">
                
                <!-- Preview Canvas Section -->
                <div class="afi-preview-canvas-container">
                    <!-- The Canvas -->
                    <?php 
                        $cafi_tmpl_data = cafi_get_current_template_data($cafi_options);
                        $cafi_initial_bg_style = '';
                        if ($cafi_tmpl_data['type'] === 'color') {
                            $cafi_initial_bg_style = 'background-color: ' . esc_attr($cafi_tmpl_data['value']) . '; background-image: none;';
                        } elseif ($cafi_tmpl_data['type'] === 'gradient') {
                             $cafi_parts = explode(',', $cafi_tmpl_data['value']);
                             if (count($cafi_parts) >= 3) {
                                  $cafi_dir = trim($cafi_parts[count($cafi_parts)-1]);
                                  $cafi_css_dir = ($cafi_dir === 'vertical') ? 'to bottom' : 'to right';
                                  $cafi_colors = array_slice($cafi_parts, 0, count($cafi_parts)-1);
                                  $cafi_color_str = implode(', ', $cafi_colors);
                                  $cafi_initial_bg_style = 'background-image: linear-gradient(' . $cafi_css_dir . ', ' . $cafi_color_str . ');';
                             }
                        } else {
                            $cafi_initial_bg_style = 'background-image: url(\'' . esc_url($cafi_tmpl_data['value']) . '\');';
                        }
                    ?>
                    <div id="afi-preview-element" style="<?php echo esc_attr( $cafi_initial_bg_style ); ?>">
                        <div class="afi-preview-overlay-text" style="
                            font-family: <?php echo isset($cafi_options['font_family']) ? esc_attr($cafi_options['font_family']) : 'Inter'; ?>;
                            color: <?php echo isset($cafi_options['font_color']) ? esc_attr($cafi_options['font_color']) : '#000000'; ?>;
                        ">
                            AutoFeatured Plugin: This Is A Sample Text For Your Review
                        </div>
                    </div>
                    
                    <!-- Meta / Actions -->
                    <div class="afi-preview-meta">
                    </div>
                </div>

                <!-- Active Settings Block -->
                <div class="afi-preview-settings-block">
                    <h4 class="afi-block-title">Active Settings</h4>
                    
                    <div class="afi-setting-row">
                        <div class="afi-setting-icon-box">
                            <span class="material-symbols-outlined">style</span>
                        </div>
                        <div class="afi-setting-info">
                            <span class="afi-setting-label">Template</span>
                            <span class="afi-setting-value" id="afi-preview-template-name">
                                <?php echo isset($cafi_options['template']) ? esc_html($cafi_options['template']) : 'Default'; ?>
                            </span>
                        </div>
                    </div>

                    <div class="afi-setting-row">
                        <div class="afi-setting-icon-box">
                            <span class="material-symbols-outlined">aspect_ratio</span>
                        </div>
                        <div class="afi-setting-info">
                            <span class="afi-setting-label">Resolution</span>
                            <span class="afi-setting-value">1200 × 630 px</span>
                        </div>
                    </div>
                </div>

                <!-- Template Thumbnail Block -->
                 <div class="afi-preview-settings-block">
                    <h4 class="afi-block-title">Template Thumbnail</h4>
                    <div class="afi-template-row">
                         <div class="afi-mini-thumbnail" id="afi-preview-mini-thumb" style="background-image: url('<?php echo esc_url( cafi_get_current_template_url($cafi_options) ); ?>');"></div>
                         <div class="afi-setting-info">
                            <span class="afi-setting-value" id="afi-preview-template-name-2">
                                <?php echo isset($cafi_options['template']) ? esc_html($cafi_options['template']) : 'Default'; ?>
                            </span>
                             <a href="#" class="afi-btn-link-sm afi-change-template-link">Change Template</a>
                         </div>
                    </div>
                </div>

            </div>
        </div>

    </aside>

</div>
