<?php
declare(strict_types=1);

require __DIR__ . '/../app/bootstrap.php';
$pdo = app_db();

$pageTitle = 'Condition Database | Medical Tracker';
$activePage = 'conditions';
$searchQuery = trim((string) ($_GET['q'] ?? ''));
$library = library_search_payload(['q' => $searchQuery, 'limit' => 500]);
$conditions = $library['medicalConditions'];
$totalConditions = (int) db_value($pdo, 'SELECT COUNT(*) FROM medical_conditions');

require __DIR__ . '/partials/site-header.php';
?>
<main>
    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">Condition database</p>
            <h1>Search conditions by name, category, overview, and tracking context.</h1>
            <p>Condition entries are concise reference summaries with MedlinePlus source links. They support tracking and discussion, not diagnosis or treatment decisions.</p>
        </div>

        <form class="library-search" method="get" action="/workouts.php">
            <label>
                <span>Search conditions</span>
                <input name="q" value="<?= htmlspecialchars($searchQuery, ENT_QUOTES) ?>" placeholder="Try diabetes, asthma, migraine, kidney, depression, arthritis">
            </label>
            <button class="site-primary" type="submit">Search</button>
            <?php if ($searchQuery !== ''): ?>
                <a class="site-secondary" href="/workouts.php">Clear</a>
            <?php endif; ?>
        </form>

        <div class="library-stats" aria-label="Condition result count">
            <article><strong><?= count($conditions) ?></strong><span><?= $searchQuery === '' ? 'shown of ' . number_format($totalConditions) . ' conditions' : 'condition results' ?></span></article>
        </div>
    </section>

    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">Catalog directory</p>
            <h2>Condition records</h2>
        </div>

        <?php if (!$conditions): ?>
            <article class="empty-state">
                <h3>No conditions found.</h3>
                <p>Try a diagnosis name, body system, symptom, medicine, lab, or tracking phrase.</p>
            </article>
        <?php else: ?>
            <div class="library-list">
                <?php foreach ($conditions as $condition): ?>
                    <article class="library-row">
                        <div class="library-row-main">
                            <h3><?= htmlspecialchars((string) $condition['name'], ENT_QUOTES) ?></h3>
                            <p><?= htmlspecialchars((string) $condition['overview'], ENT_QUOTES) ?></p>
                            <div class="library-row-meta">
                                <span><?= htmlspecialchars((string) $condition['category'], ENT_QUOTES) ?></span>
                                <span><?= htmlspecialchars((string) $condition['source_name'], ENT_QUOTES) ?></span>
                            </div>
                        </div>
                        <div class="library-row-actions">
                            <a class="site-primary" href="<?= htmlspecialchars(condition_detail_path($condition), ENT_QUOTES) ?>">View condition</a>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </section>
</main>
<?php require __DIR__ . '/partials/site-footer.php'; ?>
