<?php
declare(strict_types=1);

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

$pageTitle = 'Medical Terminology Database | Medical Tracker';
$activePage = 'terminology';
$searchQuery = trim((string) ($_GET['q'] ?? ''));
$library = library_search_payload(['q' => $searchQuery, 'limit' => 500]);
$terms = $library['medicalTerms'];
$conditions = $library['medicalConditions'];
$symptoms = $library['medicalSymptoms'];
$medicines = $library['medicationCatalog'];
$studies = $library['researchStudies'];
$totalMedicines = (int) db_value($pdo, 'SELECT COUNT(*) FROM medication_catalog');
$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">Searchable medical library</p>
            <h1>Search symptoms, terms, conditions, medicines, chemical names, and brands.</h1>
            <p>Entries are concise app-authored summaries with links to NIH/NLM systems such as MedlinePlus, RxNorm, DailyMed, PubChem, PubMed, and ClinicalTrials.gov. This is a tracking/reference aid, not medical advice.</p>
        </div>

        <form class="library-search" method="get" action="/medical.php">
            <label>
                <span>Search medical library</span>
                <input name="q" value="<?= htmlspecialchars($searchQuery, ENT_QUOTES) ?>" placeholder="Try chest pain, rash, Lipitor, blood pressure, CBC, metformin">
            </label>
            <button class="site-primary" type="submit">Search</button>
            <?php if ($searchQuery !== ''): ?>
                <a class="site-secondary" href="/medical.php">Clear</a>
            <?php endif; ?>
        </form>

        <div class="library-stats" aria-label="Search result counts">
            <article><strong><?= count($medicines) ?></strong><span><?= $searchQuery === '' ? 'medicines shown of ' . number_format($totalMedicines) : 'medicine results' ?></span></article>
            <article><strong><?= count($symptoms) ?></strong><span><?= $searchQuery === '' ? 'symptoms shown of ' . number_format($totalSymptoms) : 'symptom results' ?></span></article>
            <article><strong><?= count($terms) ?></strong><span>terms</span></article>
            <article><strong><?= count($conditions) ?></strong><span>conditions</span></article>
            <article><strong><?= count($studies) ?></strong><span>study links</span></article>
        </div>

        <?php if (!$conditions): ?>
            <article class="empty-state">
                <h3>No conditions found.</h3>
                <p>Try another medicine, symptom, lab, or tracking term.</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>

    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">Symptoms</p>
            <h2>Symptom reference 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>

    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">Terminology</p>
            <h2>Plain-language medical terms</h2>
        </div>
        <?php if (!$terms): ?>
            <article class="empty-state">
                <h3>No terms found.</h3>
                <p>Try a broader medical word, lab name, symptom, or abbreviation.</p>
            </article>
        <?php else: ?>
            <div class="library-list">
                <?php foreach ($terms as $term): ?>
                    <article class="library-row">
                        <div class="library-row-main">
                            <h3><?= htmlspecialchars((string) $term['term'], ENT_QUOTES) ?></h3>
                            <p><?= htmlspecialchars((string) $term['plain_language_definition'], ENT_QUOTES) ?></p>
                            <div class="library-row-meta">
                                <span><?= htmlspecialchars((string) $term['category'], ENT_QUOTES) ?></span>
                                <span><?= htmlspecialchars((string) $term['source_name'], ENT_QUOTES) ?></span>
                            </div>
                        </div>
                        <div class="library-row-actions">
                            <a class="site-primary" href="<?= htmlspecialchars(term_detail_path($term), ENT_QUOTES) ?>">View term</a>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </section>

    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">Medication catalog</p>
            <h2>Medicines with chemical, brand, RxNorm, DailyMed, and PubChem links</h2>
        </div>
        <?php if (!$medicines): ?>
            <article class="empty-state">
                <h3>No medicines found.</h3>
                <p>Try a generic name, brand name, chemical name, or medication class.</p>
            </article>
        <?php else: ?>
            <div class="library-list">
                <?php foreach ($medicines as $medicine): ?>
                    <article class="library-row">
                        <div class="library-row-main">
                            <h3><?= htmlspecialchars(ucfirst((string) $medicine['generic_name']), ENT_QUOTES) ?></h3>
                            <p><?= htmlspecialchars(medicine_used_for_text($medicine), ENT_QUOTES) ?></p>
                            <div class="library-row-meta">
                                <span><?= htmlspecialchars((string) ($medicine['class_name'] ?: 'Medication'), ENT_QUOTES) ?></span>
                                <span>Active: <?= htmlspecialchars((string) ($medicine['active_ingredient'] ?: $medicine['generic_name']), ENT_QUOTES) ?></span>
                                <span>Brands: <?= htmlspecialchars((string) ($medicine['brand_names'] ?: 'Vary by market'), ENT_QUOTES) ?></span>
                            </div>
                        </div>
                        <div class="library-row-actions">
                            <a class="site-primary" href="<?= htmlspecialchars(medicine_detail_path($medicine), ENT_QUOTES) ?>">View medicine</a>
                        </div>
                    </article>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </section>

    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">NIH/NLM study links</p>
            <h2>Research categories connected to the listed medicines</h2>
        </div>
        <div class="term-table">
            <?php foreach ($studies as $study): ?>
                <article>
                    <div>
                        <h3><?= htmlspecialchars($study['title'], ENT_QUOTES) ?></h3>
                        <span><?= htmlspecialchars($study['category'] . ' / ' . $study['related_medicine'], ENT_QUOTES) ?></span>
                    </div>
                    <p><?= htmlspecialchars($study['summary'], ENT_QUOTES) ?></p>
                    <div class="source-links">
                        <a href="<?= htmlspecialchars($study['source_url'], ENT_QUOTES) ?>"><?= htmlspecialchars($study['nih_source'], ENT_QUOTES) ?></a>
                        <a href="<?= htmlspecialchars($study['pubmed_url'], ENT_QUOTES) ?>">PubMed</a>
                        <a href="<?= htmlspecialchars($study['clinicaltrials_url'], ENT_QUOTES) ?>">ClinicalTrials.gov</a>
                    </div>
                </article>
            <?php endforeach; ?>
        </div>
    </section>
</main>
<?php require __DIR__ . '/partials/site-footer.php'; ?>
