<?php
declare(strict_types=1);

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

$term = find_medical_term_item($pdo, $_GET);
$pageTitle = $term
    ? (string) $term['term'] . ' | Medical Term | Medical Tracker'
    : 'Medical Term Not Found | Medical Tracker';
$activePage = 'terminology';

$relatedMedicines = [];
$relatedConditions = [];
$relatedStudies = [];
if ($term) {
    $related = library_search_payload(['q' => (string) $term['term'], 'limit' => 8]);
    $relatedMedicines = $related['medicationCatalog'];
    $relatedConditions = $related['medicalConditions'];
    $relatedStudies = $related['researchStudies'];
} else {
    http_response_code(404);
}

require __DIR__ . '/partials/site-header.php';
?>
<main>
    <?php if (!$term): ?>
        <section class="admin-public-shell record-shell">
            <p class="site-eyebrow">Medical terminology</p>
            <h1>Term not found.</h1>
            <p>The requested terminology link does not match a current database record.</p>
            <a class="site-primary" href="/medical.php">Search terminology</a>
        </section>
    <?php else: ?>
        <section class="admin-public-shell record-shell">
            <div class="record-heading">
                <div>
                    <p class="site-eyebrow">Medical term</p>
                    <h1><?= htmlspecialchars((string) $term['term'], ENT_QUOTES) ?></h1>
                    <p><?= htmlspecialchars((string) $term['plain_language_definition'], ENT_QUOTES) ?></p>
                </div>
                <div class="record-actions">
                    <a class="site-primary" href="/tracker.php">Open tracker</a>
                    <a class="site-secondary" href="/medical.php?q=<?= rawurlencode((string) $term['term']) ?>">Library search</a>
                </div>
            </div>

            <div class="record-meta">
                <span><?= htmlspecialchars((string) $term['category'], ENT_QUOTES) ?></span>
                <span>ID <?= htmlspecialchars((string) $term['id'], ENT_QUOTES) ?></span>
            </div>
        </section>

        <section class="admin-public-shell">
            <div class="record-detail-grid">
                <article class="record-field wide">
                    <span>Clinical Context</span>
                    <p><?= htmlspecialchars((string) $term['clinical_context'], ENT_QUOTES) ?></p>
                </article>
                <article class="record-field wide">
                    <span>Source</span>
                    <div class="source-links">
                        <a href="<?= htmlspecialchars((string) $term['source_url'], ENT_QUOTES) ?>"><?= htmlspecialchars((string) $term['source_name'], ENT_QUOTES) ?></a>
                    </div>
                </article>
            </div>
        </section>

        <?php if ($relatedConditions || $relatedMedicines || $relatedStudies): ?>
            <section class="admin-public-shell">
                <div class="site-section-heading">
                    <p class="site-eyebrow">Connected records</p>
                    <h2>Database matches for this term</h2>
                </div>
                <div class="library-list">
                    <?php foreach ($relatedConditions 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>
                                </div>
                            </div>
                            <div class="library-row-actions">
                                <a class="site-secondary" href="<?= htmlspecialchars(condition_detail_path($condition), ENT_QUOTES) ?>">View condition</a>
                            </div>
                        </article>
                    <?php endforeach; ?>

                    <?php foreach ($relatedMedicines 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><?= htmlspecialchars((string) ($medicine['brand_names'] ?: 'Brands vary'), ENT_QUOTES) ?></span>
                                </div>
                            </div>
                            <div class="library-row-actions">
                                <a class="site-secondary" href="<?= htmlspecialchars(medicine_detail_path($medicine), ENT_QUOTES) ?>">View medicine</a>
                            </div>
                        </article>
                    <?php endforeach; ?>

                    <?php foreach ($relatedStudies as $study): ?>
                        <article class="library-row">
                            <div class="library-row-main">
                                <h3><?= htmlspecialchars((string) $study['title'], ENT_QUOTES) ?></h3>
                                <p><?= htmlspecialchars((string) $study['summary'], ENT_QUOTES) ?></p>
                                <div class="library-row-meta">
                                    <span><?= htmlspecialchars((string) $study['category'], ENT_QUOTES) ?></span>
                                    <span><?= htmlspecialchars((string) $study['related_medicine'], ENT_QUOTES) ?></span>
                                </div>
                            </div>
                            <div class="library-row-actions">
                                <a class="site-secondary" href="<?= htmlspecialchars((string) $study['source_url'], ENT_QUOTES) ?>">Study link</a>
                            </div>
                        </article>
                    <?php endforeach; ?>
                </div>
            </section>
        <?php endif; ?>
    <?php endif; ?>
</main>
<?php require __DIR__ . '/partials/site-footer.php'; ?>
