<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/bootstrap.php';
require_once __DIR__ . '/../includes/admin_layout.php';

require_admin();

$products = store_read('products');
$orders = store_read('orders');
$comments = store_read('comments');

$activeProducts = count(array_filter($products, static fn(array $p): bool => ($p['status'] ?? '') === 'active'));
$pendingOrders = count(array_filter($orders, static fn(array $o): bool => ($o['payment_status'] ?? '') !== 'odendi'));
$paidOrders = count(array_filter($orders, static fn(array $o): bool => ($o['payment_status'] ?? '') === 'odendi'));

usort($orders, static fn(array $a, array $b): int => strcmp($b['created_at'] ?? '', $a['created_at'] ?? ''));
$recentOrders = array_slice($orders, 0, 5);

usort($products, static fn(array $a, array $b): int => strcmp($b['created_at'] ?? '', $a['created_at'] ?? ''));
$recentProducts = array_slice($products, 0, 5);

admin_layout_start('Özet', 'dashboard');
?>

<section class="admin-stats">
    <div class="admin-stat">
        <div class="label">Aktif ürün</div>
        <div class="value"><?= $activeProducts ?></div>
        <div class="meta">Toplam <?= count($products) ?> ürün</div>
    </div>
    <div class="admin-stat">
        <div class="label">Bekleyen sipariş</div>
        <div class="value"><?= $pendingOrders ?></div>
        <div class="meta"><?= $paidOrders ?> ödendi</div>
    </div>
    <div class="admin-stat">
        <div class="label">Yorum</div>
        <div class="value"><?= count($comments) ?></div>
        <div class="meta">Ürün sayfalarından</div>
    </div>
</section>

<section class="admin-panel">
    <div class="admin-panel-head">
        <h2>Son siparişler</h2>
        <a class="admin-btn ghost sm" href="<?= e(url('admin/siparisler.php')) ?>">Tümü</a>
    </div>
    <?php if (!$recentOrders): ?>
        <p class="hint">Henüz sipariş yok.</p>
    <?php else: ?>
        <?php foreach ($recentOrders as $o): ?>
            <div class="admin-order">
                <div class="admin-order-head">
                    <strong><?= e($o['customer_name'] ?? $o['phone'] ?? 'Misafir') ?></strong>
                    <span class="admin-badge <?= (($o['payment_status'] ?? '') === 'odendi') ? 'paid' : 'wait' ?>">
                        <?= e(($o['payment_status'] ?? 'bekliyor') === 'odendi' ? 'Ödendi' : 'Bekliyor') ?>
                    </span>
                </div>
                <p><?= e(money($o['total'] ?? 0)) ?> · <?= e($o['id'] ?? '') ?></p>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</section>

<section class="admin-panel">
    <div class="admin-panel-head">
        <h2>Son ürünler</h2>
        <a class="admin-btn ghost sm" href="<?= e(url('admin/urunler.php')) ?>">Tümü</a>
    </div>
    <div class="admin-table">
        <?php if (!$recentProducts): ?>
            <p class="hint">Ürün yok. İlk ürününü ekle.</p>
        <?php endif; ?>
        <?php foreach ($recentProducts as $p): ?>
            <div class="admin-row">
                <img src="<?= e(product_image_url($p)) ?>" alt="">
                <div>
                    <strong><?= e($p['title'] ?? '') ?></strong>
                    <div class="hint"><?= e(money($p['price'] ?? 0)) ?> · <?= admin_status_badge((string) ($p['status'] ?? 'active')) ?></div>
                </div>
                <div class="admin-actions">
                    <a class="admin-btn ghost sm" href="<?= e(url('admin/urun-duzenle.php?id=' . urlencode((string) $p['id']))) ?>">Düzenle</a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</section>

<?php admin_layout_end(); ?>
