<?php
declare(strict_types=1);

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

$pageTitle = 'Symptom Database | Medical Tracker';
$activePage = 'symptoms';
$searchQuery = trim((string) ($_GET['q'] ?? ''));
$library = library_search_payload(['q' => $searchQuery, 'limit' => 500]);
$symptoms = $library['medicalSymptoms'];
$totalSymptoms = (int) db_value($pdo, 'SELECT COUNT(*) FROM medical_symptoms');

require __DIR__ . '/partials/site-header.php';
?>
<main>
    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">Symptom database</p>
            <h1>Search symptoms by name, body system, tracking context, and urgency notes.</h1>
            <p>These entries are 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="/symptoms.php">
            <label>
                <span>Search symptoms</span>
                <input name="q" value="<?= htmlspecialchars($searchQuery, ENT_QUOTES) ?>" placeholder="Try chest pain, rash, nausea, dizziness, urinary, sleep">
            </label>
            <button class="site-primary" type="submit">Search</button>
            <?php if ($searchQuery !== ''): ?>
                <a class="site-secondary" href="/symptoms.php">Clear</a>
            <?php endif; ?>
        </form>

        <div class="library-stats" aria-label="Symptom result count">
            <article><strong><?= count($symptoms) ?></strong><span><?= $searchQuery === '' ? 'shown of ' . number_format($totalSymptoms) . ' symptoms' : 'symptom results' ?></span></article>
        </div>
    </section>

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

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