<?php
/**
 * Admin Layout Template
 * 
 * Main layout wrapper for all admin pages following UX guidelines
 * 
 * @package AIProductContentImageGenerator
 * @since 1.0.0
 */

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

try {
    $apcig_router = \AIProductContentImageGenerator\Plugin::getInstance()->getRouter();
    $apcig_current_page = $apcig_router->get_current_page();
    $apcig_nav_items = $apcig_router->get_navigation_items();
} catch (Exception $e) {
    $apcig_current_page = 'apcig-dashboard';
    $apcig_nav_items = [
        'apcig-dashboard' => ['title' => 'Dashboard', 'url' => '#', 'icon' => 'dashicons-dashboard'],
        'apcig-content-generator' => ['title' => 'Content', 'url' => '#', 'icon' => 'dashicons-edit-page']
    ];
}

$apcig_view_file = isset($content_file_path) ? $content_file_path : '';
?>

<div class="apcig-admin-wrap">
    <!-- Header -->
    <div class="apcig-header">
        <h1>
            <span class="dashicons dashicons-admin-tools"></span>
            <?php esc_html_e('AI Product Content & Image Generator', 'contengen-ai-product-content-image-generator'); ?>
        </h1>
    </div>

    <!-- Alerts Container -->
    <div class="apcig-alerts-container"></div>

    <!-- Main Layout -->
    <div class="apcig-layout">
        <!-- Sidebar Navigation -->
        <div class="apcig-sidebar">
            <nav class="apcig-nav">
                <ul>
                    <?php foreach ($apcig_nav_items as $apcig_page_slug => $apcig_nav_item): ?>
                    <li>
                        <a href="<?php echo esc_url($apcig_nav_item['url']); ?>" 
                           class="<?php echo esc_attr($apcig_current_page === $apcig_page_slug ? 'current' : ''); ?>"
                           data-page="<?php echo esc_attr($apcig_page_slug); ?>">
                            <span class="dashicons <?php echo esc_attr($apcig_nav_item['icon']); ?>"></span>
                            <?php echo esc_html($apcig_nav_item['title']); ?>
                        </a>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </nav>

            <!-- Quick Stats Widget -->
            <div class="apcig-sidebar-widget">
                <h3><?php esc_html_e('Quick Stats', 'contengen-ai-product-content-image-generator'); ?></h3>
                <div class="apcig-quick-stats">
                    <div class="apcig-quick-stat">
                        <span class="apcig-stat-label"><?php esc_html_e('Products', 'contengen-ai-product-content-image-generator'); ?></span>
                        <span class="apcig-stat-value" id="sidebar-total-products">
                            <?php echo esc_html(wp_count_posts('product')->publish ?? 0); ?>
                        </span>
                    </div>
                    <div class="apcig-quick-stat">
                        <span class="apcig-stat-label"><?php esc_html_e('Generated Today', 'contengen-ai-product-content-image-generator'); ?></span>
                        <span class="apcig-stat-value" id="sidebar-generated-today">
                            <?php echo esc_html(get_option('apcig_content_generated_today', 0)); ?>
                        </span>
                    </div>
                </div>
            </div>

            <!-- Provider Status Widget -->
            <div class="apcig-sidebar-widget">
                <h3><?php esc_html_e('AI Providers', 'contengen-ai-product-content-image-generator'); ?></h3>
                <div class="apcig-provider-status">
                    <?php
                    try {
                        $apcig_ai_provider_manager = new \AIProductContentImageGenerator\Services\AIProviderManager();
                        $apcig_providers = $apcig_ai_provider_manager->get_providers();
                        foreach ($apcig_providers as $apcig_provider_id => $apcig_provider_config):
                            $apcig_is_configured = $apcig_ai_provider_manager->is_provider_configured($apcig_provider_id);
                    ?>
                    <div class="apcig-provider-item">
                        <span class="apcig-provider-name"><?php echo esc_html($apcig_provider_config['name']); ?></span>
                        <span class="apcig-provider-indicator <?php echo esc_attr($apcig_is_configured ? 'connected' : 'disconnected'); ?>">
                            <span class="dashicons dashicons-<?php echo esc_attr($apcig_is_configured ? 'yes-alt' : 'warning'); ?>"></span>
                        </span>
                    </div>
                    <?php 
                        endforeach;
                    } catch (Exception $e) {
                        // Show error message or fallback
                        echo '<p>' . esc_html__('Unable to load provider status.', 'contengen-ai-product-content-image-generator') . '</p>';
                    }
                    ?>

                </div>
            </div>
        </div>

        <!-- Main Content Area -->
        <div class="apcig-main-content">
            <?php
            // Include the specific view file
            if (!empty($apcig_view_file) && file_exists($apcig_view_file)) {
                include $apcig_view_file;
            } else {
                // Fallback content
                ?>
                <div class="apcig-card">
                    <div class="apcig-card-header">
                        <h3><?php esc_html_e('Page Not Found', 'contengen-ai-product-content-image-generator'); ?></h3>
                    </div>
                    <div class="apcig-card-body">
                        <p><?php esc_html_e('The requested page could not be found.', 'contengen-ai-product-content-image-generator'); ?></p>
                        <a href="<?php echo esc_url(admin_url('admin.php?page=apcig-dashboard')); ?>" class="apcig-btn apcig-btn-primary">
                            <?php esc_html_e('Go to Dashboard', 'contengen-ai-product-content-image-generator'); ?>
                        </a>
                    </div>
                </div>
                <?php
            }
            ?>
        </div>
    </div>
</div>