<?php
declare(strict_types=1);

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

library_bootstrap();

$documents = library_fetch_documents(false, []);
$metrics = library_catalog_metrics();
$lang = library_get_language();

admin_render_header(library_trans('dashboard'), 'dashboard');
?>

<!-- Metrics -->
<div class="row g-4 mb-5">
    <div class="col-md-4">
        <div class="card shadow-sm border-0 h-100">
            <div class="card-body">
                <h6 class="text-uppercase text-secondary small fw-bold"><?= library_trans('total_documents') ?></h6>
                <div class="d-flex align-items-center justify-content-between mt-3">
                    <h2 class="display-6 fw-bold mb-0 text-primary"><?= number_format((int) ($metrics['total_count'] ?? 0)) ?></h2>
                    <i class="bi bi-file-earmark-text fs-1 text-primary opacity-25"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card shadow-sm border-0 h-100">
            <div class="card-body">
                <h6 class="text-uppercase text-secondary small fw-bold"><?= library_trans('public_titles') ?></h6>
                <div class="d-flex align-items-center justify-content-between mt-3">
                    <h2 class="display-6 fw-bold mb-0 text-success"><?= number_format((int) ($metrics['public_count'] ?? 0)) ?></h2>
                    <i class="bi bi-globe fs-1 text-success opacity-25"></i>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card shadow-sm border-0 h-100">
            <div class="card-body">
                <h6 class="text-uppercase text-secondary small fw-bold"><?= library_trans('private_titles') ?></h6>
                <div class="d-flex align-items-center justify-content-between mt-3">
                    <h2 class="display-6 fw-bold mb-0 text-info"><?= number_format((int) ($metrics['private_count'] ?? 0)) ?></h2>
                    <i class="bi bi-lock fs-1 text-info opacity-25"></i>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <!-- Recent Documents -->
    <div class="col-lg-8">
        <div class="card shadow-sm border-0 mb-4">
            <div class="card-header bg-white py-3 border-bottom">
                <h5 class="card-title mb-0"><?= library_trans('recent_documents') ?></h5>
            </div>
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th class="ps-4"><?= library_trans('title_author') ?></th>
                            <th><?= library_trans('type_category') ?></th>
                            <th><?= library_trans('visibility') ?></th>
                            <th class="text-end pe-4"><?= library_trans('actions') ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($documents)): ?>
                            <tr><td colspan="4" class="text-center py-5 text-muted"><?= library_trans('no_documents_found') ?></td></tr>
                        <?php else: ?>
                            <?php foreach (array_slice($documents, 0, 10) as $doc): ?>
                                <?php
                                    $title = library_localized_document_title($doc, $lang);
                                    $typeLabel = library_localized_value($doc['type_en'] ?? null, $doc['type_ar'] ?? null, $lang);
                                    $categoryLabel = library_localized_value($doc['cat_en'] ?? $doc['category'] ?? null, $doc['cat_ar'] ?? $doc['category_ar'] ?? null, $lang);
                                ?>
                                <tr>
                                    <td class="ps-4">
                                        <div class="fw-medium text-dark" dir="<?= library_text_dir($title, $lang) ?>"><?= h($title) ?></div>
                                        <?php if (!empty($doc['author'])): ?>
                                            <div class="small text-primary mt-1"><i class="bi bi-person me-1"></i><?= h($doc['author']) ?></div>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <?php if ($typeLabel !== ''): ?>
                                            <span class="badge bg-info bg-opacity-10 text-info border border-info border-opacity-25 mb-1" dir="<?= library_text_dir($typeLabel, $lang) ?>"><?= h($typeLabel) ?></span><br>
                                        <?php endif; ?>
                                        <?php if ($categoryLabel !== ''): ?>
                                            <span class="badge bg-light text-dark border" dir="<?= library_text_dir($categoryLabel, $lang) ?>">
                                                <?= h($categoryLabel) ?>
                                            </span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <span class="badge bg-<?= $doc['visibility'] === 'public' ? 'success' : 'secondary' ?>">
                                            <?= h(library_visibility_label($doc['visibility'])) ?>
                                        </span>
                                    </td>
                                    <td class="text-end pe-4">
                                        <a href="/document.php?id=<?= $doc['id'] ?>" class="btn btn-sm btn-outline-primary" target="_blank">
                                            <i class="bi bi-eye"></i> <?= library_trans('view') ?>
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <div class="card-footer bg-white text-center py-3">
                <small class="text-muted"><?= library_trans('showing_last_10') ?></small>
            </div>
        </div>
    </div>

    <!-- Material Entry Promo -->
    <div class="col-lg-4">
        <div class="card shadow-sm border-0">
            <div class="card-header bg-primary text-white py-3">
                <h5 class="card-title mb-0"><i class="bi bi-folder2-open me-2"></i><?= library_trans('material_entry') ?></h5>
            </div>
            <div class="card-body p-4 text-center">
                <p class="text-secondary mb-4">
                    <?= library_trans('manage_documents_desc') ?>
                </p>
                <a href="/admin_documents.php" class="btn btn-primary w-100">
                    <i class="bi bi-plus-lg me-2"></i> <?= library_trans('manage_documents') ?>
                </a>
            </div>
        </div>
    </div>
</div>

<?php admin_render_footer(); ?>
