<?php

require_once __DIR__ . '/../app/auth.php';
require_once __DIR__ . '/../app/layout.php';
require_once __DIR__ . '/../app/helpers.php';
require_once __DIR__ . '/../app/analytics.php';

$user = sparrow_require_backyard_login();

$total_events = sparrow_analytics_total_events();
$gallery_page_views = sparrow_analytics_count_event_type('page_view');
$gallery_opens = sparrow_analytics_count_event_type('gallery_lightbox_open');
$blog_post_views = sparrow_analytics_count_event_type('blog_post_view');

$top_gallery_item = sparrow_analytics_most_opened_gallery_item();
$top_blog_post = sparrow_analytics_most_viewed_blog_post();
$top_category = sparrow_analytics_top_category();
$recent_events = sparrow_analytics_recent_events(25);

sparrow_backyard_header('Analytics');

?>

<section class="card">
    <div class="page-heading-row">
        <div>
            <h1>Analytics</h1>
            <p class="muted">
                Simple public website activity for gallery and product stories.
            </p>
        </div>
    </div>
</section>

<section class="analytics-card-grid">
    <div class="analytics-stat-card">
        <span class="analytics-stat-label">Total Events</span>
        <strong><?php echo number_format($total_events); ?></strong>
    </div>

    <div class="analytics-stat-card">
        <span class="analytics-stat-label">Page Views</span>
        <strong><?php echo number_format($gallery_page_views); ?></strong>
    </div>

    <div class="analytics-stat-card">
        <span class="analytics-stat-label">Gallery Opens</span>
        <strong><?php echo number_format($gallery_opens); ?></strong>
    </div>

    <div class="analytics-stat-card">
        <span class="analytics-stat-label">Blog Story Views</span>
        <strong><?php echo number_format($blog_post_views); ?></strong>
    </div>
</section>

<section class="analytics-card-grid">
    <div class="analytics-info-card">
        <h2>Most Opened Gallery Item</h2>

        <?php if ($top_gallery_item): ?>
            <p>
                <strong><?php echo sparrow_escape($top_gallery_item['entity_title'] ?: 'Untitled'); ?></strong>
            </p>
            <p class="muted">
                <?php echo number_format((int) $top_gallery_item['total']); ?> opens
                <?php if (!empty($top_gallery_item['entity_category'])): ?>
                    · <?php echo sparrow_escape($top_gallery_item['entity_category']); ?>
                <?php endif; ?>
            </p>
        <?php else: ?>
            <p class="muted">No gallery opens tracked yet.</p>
        <?php endif; ?>
    </div>

    <div class="analytics-info-card">
        <h2>Most Viewed Blog Story</h2>

        <?php if ($top_blog_post): ?>
            <p>
                <strong><?php echo sparrow_escape($top_blog_post['entity_title'] ?: 'Untitled'); ?></strong>
            </p>
            <p class="muted">
                <?php echo number_format((int) $top_blog_post['total']); ?> views
            </p>
        <?php else: ?>
            <p class="muted">No blog story views tracked yet.</p>
        <?php endif; ?>
    </div>

    <div class="analytics-info-card">
        <h2>Top Category</h2>

        <?php if ($top_category): ?>
            <p>
                <strong><?php echo sparrow_escape($top_category['entity_category']); ?></strong>
            </p>
            <p class="muted">
                <?php echo number_format((int) $top_category['total']); ?> tracked events
            </p>
        <?php else: ?>
            <p class="muted">No category activity tracked yet.</p>
        <?php endif; ?>
    </div>
</section>

<section class="card">
    <h2>Recent Activity</h2>

    <?php if (!$recent_events): ?>
        <p class="muted">No analytics activity yet.</p>
    <?php else: ?>
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Event</th>
                        <th>Item</th>
                        <th>Category</th>
                        <th>Page</th>
                    </tr>
                </thead>

                <tbody>
                    <?php foreach ($recent_events as $event): ?>
                        <tr>
                            <td>
                                <?php echo sparrow_escape(date('M j, Y g:i A', strtotime($event['created_at']))); ?>
                            </td>
                            <td>
                                <span class="analytics-pill">
                                    <?php echo sparrow_escape($event['event_type']); ?>
                                </span>
                            </td>
                            <td>
                                <?php echo sparrow_escape($event['entity_title'] ?: ''); ?>
                            </td>
                            <td>
                                <?php echo sparrow_escape($event['entity_category'] ?: ''); ?>
                            </td>
                            <td>
                                <?php echo sparrow_escape($event['page_url'] ?: ''); ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</section>

<?php sparrow_backyard_footer(); ?>