<?php
/**
 * Dashboard View Template
 * 
 * @package AIProductContentImageGenerator
 * @since 1.0.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Get dashboard statistics - these will be updated dynamically via AJAX
$apcig_today = current_time('Y-m-d');
$apcig_today_key = 'apcig_api_usage_' . $apcig_today;
$apcig_daily_usage = get_option($apcig_today_key, []);
$apcig_api_calls_today = array_sum($apcig_daily_usage);

$apcig_stats = [
    'total_products' => wp_count_posts('product')->publish ?? 0,
    'generated_content' => get_option('apcig_total_content_generated', 0),
    'generated_images' => get_option('apcig_total_images_generated', 0),
    'api_calls_today' => $apcig_api_calls_today
];

// Get recent activity
$apcig_recent_activity = \get_option('apcig_recent_activity', []);

// Check if WooCommerce is active
$apcig_woocommerce_active = \class_exists('WooCommerce');
?>

<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 Content -->
    <div class="apcig-main-content">
        <?php if (!$apcig_woocommerce_active): ?>
            <div class="apcig-alert apcig-alert-warning">
                <span class="dashicons dashicons-warning"></span>
                <span><?php \esc_html_e('WooCommerce is required for this plugin to work properly. Please install and activate WooCommerce.', 'contengen-ai-product-content-image-generator'); ?></span>
            </div>
        <?php endif; ?>

        <!-- Welcome Section -->
        <div class="apcig-welcome">
            <h2><?php \esc_html_e('Welcome to AI Product Generator', 'contengen-ai-product-content-image-generator'); ?></h2>
            <p><?php \esc_html_e('Generate compelling product content and stunning images using advanced AI technology. Get started by configuring your AI providers or jump right into content generation.', 'contengen-ai-product-content-image-generator'); ?></p>
        </div>

        <!-- Statistics Cards -->
        <div class="apcig-stats">
            <div class="apcig-stat-card">
                <h3>
                    <span class="dashicons dashicons-products"></span>
                    <?php \esc_html_e('Total Products', 'contengen-ai-product-content-image-generator'); ?>
                </h3>
                <div class="apcig-stat-number" data-stat="total_products"><?php echo \esc_html($apcig_stats['total_products']); ?></div>
                <div class="apcig-stat-change">+<?php echo \esc_html(\wp_count_posts('product')->draft ?? 0); ?> <?php \esc_html_e('drafts', 'contengen-ai-product-content-image-generator'); ?></div>
            </div>

            <div class="apcig-stat-card">
                <h3>
                    <span class="dashicons dashicons-edit-page"></span>
                    <?php \esc_html_e('Content Generated', 'contengen-ai-product-content-image-generator'); ?>
                </h3>
                <div class="apcig-stat-number" data-stat="generated_content"><?php echo \esc_html($apcig_stats['generated_content']); ?></div>
                <div class="apcig-stat-change">+<?php echo \esc_html(\get_option('apcig_content_generated_today', 0)); ?> <?php \esc_html_e('today', 'contengen-ai-product-content-image-generator'); ?></div>
            </div>

            <div class="apcig-stat-card">
                <h3>
                    <span class="dashicons dashicons-format-image"></span>
                    <?php \esc_html_e('Images Generated', 'contengen-ai-product-content-image-generator'); ?>
                </h3>
                <div class="apcig-stat-number" data-stat="generated_images"><?php echo \esc_html($apcig_stats['generated_images']); ?></div>
                <div class="apcig-stat-change">+<?php echo \esc_html(\get_option('apcig_images_generated_today', 0)); ?> <?php \esc_html_e('today', 'contengen-ai-product-content-image-generator'); ?></div>
            </div>

            <div class="apcig-stat-card">
                <h3>
                    <span class="dashicons dashicons-cloud"></span>
                    <?php \esc_html_e('API Calls Today', 'contengen-ai-product-content-image-generator'); ?>
                </h3>
                <div class="apcig-stat-number" data-stat="api_calls_today"><?php echo \esc_html($apcig_stats['api_calls_today']); ?></div>
                <div class="apcig-stat-change" id="apcig-api-breakdown">
                    <?php 
                    if (!empty($apcig_daily_usage)) {
                        $apcig_breakdown_parts = [];
                        foreach ($apcig_daily_usage as $apcig_provider => $apcig_count) {
                            $apcig_breakdown_parts[] = ucfirst($apcig_provider) . ': ' . $apcig_count;
                        }
                        echo esc_html(implode(' | ', $apcig_breakdown_parts));
                    } else {
                        esc_html_e('No API calls today', 'contengen-ai-product-content-image-generator');
                    }
                    ?>
                </div>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="apcig-quick-actions">
            <div class="apcig-action-card">
                <h3>
                    <span class="dashicons dashicons-edit-page"></span>
                    <?php \esc_html_e('Generate Content', 'contengen-ai-product-content-image-generator'); ?>
                </h3>
                <p><?php \esc_html_e('Create compelling product descriptions, titles, and tags using AI. Choose from multiple tones and styles to match your brand voice.', 'contengen-ai-product-content-image-generator'); ?></p>
                <a href="<?php echo \esc_url(\admin_url('admin.php?page=apcig-content-generator')); ?>" class="apcig-btn apcig-btn-primary">
                    <span class="dashicons dashicons-plus-alt"></span>
                    <?php \esc_html_e('Start Generating', 'contengen-ai-product-content-image-generator'); ?>
                </a>
            </div>

            <div class="apcig-action-card">
                <h3>
                    <span class="dashicons dashicons-format-image"></span>
                    <?php \esc_html_e('Generate Images', 'contengen-ai-product-content-image-generator'); ?>
                </h3>
                <p><?php \esc_html_e('Create stunning product images from text descriptions or enhance existing photos with AI-powered tools and background removal.', 'contengen-ai-product-content-image-generator'); ?></p>
                <a href="<?php echo \esc_url(\admin_url('admin.php?page=apcig-image-generator')); ?>" class="apcig-btn apcig-btn-primary">
                    <span class="dashicons dashicons-camera"></span>
                    <?php \esc_html_e('Create Images', 'contengen-ai-product-content-image-generator'); ?>
                </a>
            </div>

            <div class="apcig-action-card">
                <h3>
                    <span class="dashicons dashicons-performance"></span>
                    <?php \esc_html_e('Bulk Processing', 'contengen-ai-product-content-image-generator'); ?>
                </h3>
                <p><?php \esc_html_e('Process up to 50 products at once with our intelligent bulk generation system. Perfect for large catalogs and new store setups.', 'contengen-ai-product-content-image-generator'); ?></p>
                <a href="<?php echo \esc_url(\admin_url('admin.php?page=apcig-bulk-processor')); ?>" class="apcig-btn apcig-btn-primary">
                    <span class="dashicons dashicons-update"></span>
                    <?php \esc_html_e('Bulk Process', 'contengen-ai-product-content-image-generator'); ?>
                </a>
            </div>

            <div class="apcig-action-card">
                <h3>
                    <span class="dashicons dashicons-admin-settings"></span>
                    <?php \esc_html_e('Configure Settings', 'contengen-ai-product-content-image-generator'); ?>
                </h3>
                <p><?php \esc_html_e('Set up your AI providers, customize generation parameters, and configure advanced options to get the best results for your products.', 'contengen-ai-product-content-image-generator'); ?></p>
                <a href="<?php echo \esc_url(\admin_url('admin.php?page=apcig-settings')); ?>" class="apcig-btn apcig-btn-secondary">
                    <span class="dashicons dashicons-admin-generic"></span>
                    <?php \esc_html_e('Open Settings', 'contengen-ai-product-content-image-generator'); ?>
                </a>
            </div>
        </div>

        <!-- Recent Activity -->
        <?php if (!empty($apcig_recent_activity)): ?>
        <div class="apcig-card">
            <div class="apcig-card-header">
                <h3><?php \esc_html_e('Recent Activity', 'contengen-ai-product-content-image-generator'); ?></h3>
            </div>
            <div class="apcig-card-body">
                <div class="apcig-activity-list">
                    <?php foreach (\array_slice($apcig_recent_activity, 0, 10) as $apcig_activity): ?>
                    <div class="apcig-activity-item">
                        <div class="apcig-activity-icon">
                            <span class="dashicons dashicons-<?php echo \esc_attr($apcig_activity['icon'] ?? 'admin-generic'); ?>"></span>
                        </div>
                        <div class="apcig-activity-content">
                            <div class="apcig-activity-title"><?php echo \esc_html($apcig_activity['title'] ?? ''); ?></div>
                            <div class="apcig-activity-time"><?php echo \esc_html($apcig_activity['time'] ?? ''); ?></div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>
<!-- Dashboard styles moved to admin.css -->
