<?php

declare(strict_types=1);

require_once __DIR__ . '/../app/config/bootstrap.php';
require_once __DIR__ . '/../app/core/DijikalaLayout.php';
require_once __DIR__ . '/../app/core/Auth.php';
require_once __DIR__ . '/../app/models/Product.php';

Auth::startSession();

$key = 'compare_product_ids';

if (!isset($_SESSION[$key]) || !is_array($_SESSION[$key])) {
    $_SESSION[$key] = [];
}

/** @var list<int> $ids */
$ids = $_SESSION[$key];

if (isset($_GET['add'])) {
    $pid = (int)$_GET['add'];
    if ($pid > 0 && !in_array($pid, $ids, true)) {
        $ids[] = $pid;
        if (count($ids) > 4) {
            $ids = array_slice($ids, -4);
        }
        $_SESSION[$key] = $ids;
    }
}

if (isset($_GET['remove'])) {
    $rid = (int)$_GET['remove'];
    $_SESSION[$key] = array_values(array_filter($ids, static fn ($x) => (int)$x !== $rid));
    $ids = $_SESSION[$key];
}

if (isset($_GET['clear'])) {
    $_SESSION[$key] = [];
    $ids = [];
}

$products = [];
$productModel = new Product();
foreach ($ids as $pid) {
    $row = $productModel->find((int)$pid);
    if ($row) {
        $products[] = $row;
    }
}

renderDijikalaShellStart('مقایسه محصولات | ' . APP_NAME, ['noindex' => true], '');
?>

<article class="container" style="padding:1.5rem 0 3rem;">
    <div style="display:flex;justify-content:space-between;align-items:center;flex-wrap:wrap;gap:12px;margin-bottom:16px;">
        <h2 style="margin:0;">مقایسه (حداکثر ۴ محصول)</h2>
        <?php if ($ids): ?>
            <a href="<?= htmlspecialchars(appPath('/public/compare.php')) ?>?clear=1" class="btn-link-spoiler">پاک کردن همه</a>
        <?php endif; ?>
    </div>

    <?php if (!$products): ?>
        <p>محصولی برای مقایسه انتخاب نشده. از صفحه هر محصول «افزودن به مقایسه» را بزنید.</p>
    <?php else: ?>
        <div style="overflow-x:auto;">
            <table style="width:100%;border-collapse:collapse;min-width:520px;">
                <thead>
                <tr>
                    <th style="text-align:right;padding:10px;border:1px solid #e5e7eb;">فیلد</th>
                    <?php foreach ($products as $p): ?>
                        <th style="text-align:right;padding:10px;border:1px solid #e5e7eb;vertical-align:bottom;">
                            <a href="<?= dijikalaProductUrl($p) ?>"><?= htmlspecialchars((string)$p['title']) ?></a><br>
                            <a href="<?= htmlspecialchars(appPath('/public/compare.php')) ?>?remove=<?= (int)$p['id'] ?>" class="btn-link-spoiler">حذف</a>
                        </th>
                    <?php endforeach; ?>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td style="padding:8px;border:1px solid #e5e7eb;">تصویر</td>
                    <?php foreach ($products as $p): ?>
                        <td style="padding:8px;border:1px solid #e5e7eb;"><img src="<?= dijikalaProductThumbUrl($p) ?>" alt="" style="max-width:96px;"></td>
                    <?php endforeach; ?>
                </tr>
                <tr>
                    <td style="padding:8px;border:1px solid #e5e7eb;">قیمت</td>
                    <?php foreach ($products as $p): ?>
                        <td style="padding:8px;border:1px solid #e5e7eb;"><?= number_format((float)$p['price']) ?> تومان</td>
                    <?php endforeach; ?>
                </tr>
                <tr>
                    <td style="padding:8px;border:1px solid #e5e7eb;">دسته</td>
                    <?php foreach ($products as $p): ?>
                        <td style="padding:8px;border:1px solid #e5e7eb;"><?= htmlspecialchars((string)$p['category']) ?></td>
                    <?php endforeach; ?>
                </tr>
                <tr>
                    <td style="padding:8px;border:1px solid #e5e7eb;">موجودی</td>
                    <?php foreach ($products as $p): ?>
                        <td style="padding:8px;border:1px solid #e5e7eb;"><?= (int)$p['stock'] ?></td>
                    <?php endforeach; ?>
                </tr>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</article>

<?php
renderDijikalaShellEnd('compare', 'minimal');
Performance::finish('public.compare');
