<?php
declare(strict_types=1);

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

$symptom = find_medical_symptom_item($pdo, $_GET);
$pageTitle = $symptom
    ? (string) $symptom['name'] . ' | Symptom Detail | Medical Tracker'
    : 'Symptom Not Found | Medical Tracker';
$activePage = 'symptoms';

$relatedTerms = [];
$relatedConditions = [];
$relatedMedicines = [];
$relatedStudies = [];
$linkedMedicines = [];
if ($symptom) {
    $linkedMedicines = linked_medicines_for_symptom($pdo, (int) $symptom['id']);
    $related = library_search_payload(['q' => (string) $symptom['name'], 'limit' => 8]);
    $relatedTerms = $related['medicalTerms'];
    $relatedConditions = $related['medicalConditions'];
    $relatedMedicines = $related['medicationCatalog'];
    $relatedStudies = $related['researchStudies'];
    if ($linkedMedicines) {
        $linkedIds = array_fill_keys(array_map(static fn(array $row): int => (int) $row['id'], $linkedMedicines), true);
        $relatedMedicines = array_values(array_filter(
            $relatedMedicines,
            static fn(array $row): bool => !isset($linkedIds[(int) $row['id']])
        ));
    }
} else {
    http_response_code(404);
}

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

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

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

        <?php if ($linkedMedicines): ?>
            <section class="admin-public-shell">
                <div class="site-section-heading">
                    <p class="site-eyebrow">Medicine links</p>
                    <h2>Commonly linked medicines</h2>
                </div>
                <div class="library-list">
                    <?php foreach ($linkedMedicines as $medicine): ?>
                        <article class="library-row">
                            <div class="library-row-main">
                                <h3><?= htmlspecialchars(ucfirst((string) $medicine['generic_name']), ENT_QUOTES) ?></h3>
                                <p><?= htmlspecialchars((string) ($medicine['link_note'] ?: 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['link_relationship'], 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; ?>
                </div>
            </section>
        <?php endif; ?>

        <?php if ($relatedTerms || $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 symptom</h2>
                </div>
                <div class="library-list">
                    <?php foreach ($relatedTerms 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>
                                </div>
                            </div>
                            <div class="library-row-actions">
                                <a class="site-secondary" href="<?= htmlspecialchars(term_detail_path($term), ENT_QUOTES) ?>">View term</a>
                            </div>
                        </article>
                    <?php endforeach; ?>

                    <?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>
                                </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'; ?>
