<?php
/**
 * Analytics Page Partial - Modern Design
 *
 * @package    Ai_Magic
 * @subpackage Ai_Magic/admin/partials
 */

global $wpdb;

// Get all ai_magic_logs posts
$args = array(
    'post_type' => 'ai_magic_logs',
    'posts_per_page' => -1,
    'fields' => 'ids'
);
$all_posts = get_posts($args);

// Calculate stats from post meta
$total_requests = count($all_posts);
$total_tokens = 0;
$total_cost = 0;

foreach ($all_posts as $post_id) {
    $tokens = get_post_meta($post_id, 'total_tokens', true);
    $cost = get_post_meta($post_id, '_ai_magic_cost', true);

    if ($tokens) {
        $total_tokens += intval($tokens);
    }
    if ($cost) {
        $total_cost += floatval($cost);
    }
}

// Get recent activity (last 10)
$recent_args = array(
    'post_type' => 'ai_magic_logs',
    'posts_per_page' => 10,
    'orderby' => 'date',
    'order' => 'DESC'
);
$recent_posts = get_posts($recent_args);

// Format recent activity
$recent = array();
foreach ($recent_posts as $post) {
    $recent[] = (object) array(
        'model' => get_post_meta($post->ID, 'model', true),
        'tokens_used' => get_post_meta($post->ID, 'total_tokens', true),
        'cost' => get_post_meta($post->ID, '_ai_magic_cost', true),
        'created_at' => $post->post_date
    );
}
?>
<div class="ai-magic-wizard-wrap">
    <div class="ai-magic-wizard-header">
        <span class="dashicons dashicons-chart-bar"></span>
        <h1>Analytics Dashboard</h1>
    </div>

    <!-- Stats Cards -->
    <div class="ai-magic-analytics-stats">
        <div class="ai-magic-card ai-magic-stat-card">
            <div class="ai-magic-stat-icon">
                <span class="dashicons dashicons-admin-comments"></span>
            </div>
            <div class="ai-magic-stat-content">
                <div class="ai-magic-stat-value"><?php echo number_format($total_requests ?: 0); ?></div>
                <div class="ai-magic-stat-label">Total Requests</div>
            </div>
        </div>
        <div class="ai-magic-card ai-magic-stat-card">
            <div class="ai-magic-stat-icon">
                <span class="dashicons dashicons-text-page"></span>
            </div>
            <div class="ai-magic-stat-content">
                <div class="ai-magic-stat-value"><?php echo number_format($total_tokens ?: 0); ?></div>
                <div class="ai-magic-stat-label">Total Tokens</div>
            </div>
        </div>
        <div class="ai-magic-card ai-magic-stat-card">
            <div class="ai-magic-stat-icon cost">
                <span class="dashicons dashicons-money-alt"></span>
            </div>
            <div class="ai-magic-stat-content">
                <div class="ai-magic-stat-value cost">$<?php echo number_format($total_cost ?: 0, 4); ?></div>
                <div class="ai-magic-stat-label">Total Cost</div>
            </div>
        </div>
    </div>

    <!-- Recent Activity -->
    <div class="ai-magic-card">
        <div class="ai-magic-card-header">
            <h2>Recent Activity</h2>
        </div>

        <?php if (empty($recent)): ?>
            <div class="ai-magic-empty-state">
                <span class="dashicons dashicons-chart-area"></span>
                <p>No analytics data yet</p>
                <span class="ai-magic-empty-hint">Start generating content to see usage stats</span>
            </div>
        <?php else: ?>
            <div class="ai-magic-table-wrap">
                <table class="ai-magic-table">
                    <thead>
                        <tr>
                            <th>Model</th>
                            <th>Tokens</th>
                            <th>Cost</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recent as $item): ?>
                            <tr>
                                <td><span class="ai-magic-badge"><?php echo esc_html($item->model); ?></span></td>
                                <td><?php echo number_format(intval($item->tokens_used)); ?></td>
                                <td class="cost">$<?php echo number_format(floatval($item->cost), 4); ?></td>
                                <td><?php echo date('M d, Y H:i', strtotime($item->created_at)); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>