<?php
declare(strict_types=1);

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

$medicine = find_medication_catalog_item($pdo, $_GET);
$pageTitle = $medicine
    ? ucfirst((string) $medicine['generic_name']) . ' | Medicine Detail | Medical Tracker'
    : 'Medicine Not Found | Medical Tracker';
$activePage = 'medicines';

$relatedStudies = [];
$referenceLinks = [];
$linkedConditions = [];
$linkedSymptoms = [];
$useSourceLinks = [];
if ($medicine) {
    $genericName = (string) $medicine['generic_name'];
    $like = '%' . $genericName . '%';
    $linkedConditions = linked_conditions_for_medicine($pdo, (int) $medicine['id']);
    $linkedSymptoms = linked_symptoms_for_medicine($pdo, (int) $medicine['id']);
    $relatedStudies = db_all(
        $pdo,
        "SELECT *
         FROM research_studies
         WHERE related_medicine LIKE ? OR title LIKE ? OR summary LIKE ?
         ORDER BY category, title
         LIMIT 6",
        [$like, $like, $like]
    );
    $referenceLinks = medicine_reference_links($medicine);
    $useSourceLinks = medicine_use_source_link($medicine);
} else {
    http_response_code(404);
}

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

            <div class="record-meta">
                <span><?= htmlspecialchars((string) ($medicine['class_name'] ?: 'Medication'), ENT_QUOTES) ?></span>
                <span><?= htmlspecialchars((string) ($medicine['drug_family'] ?: $medicine['source_name'] ?: 'Reference catalog'), ENT_QUOTES) ?></span>
                <span>ID <?= htmlspecialchars((string) $medicine['id'], ENT_QUOTES) ?></span>
            </div>
        </section>

        <section class="admin-public-shell">
            <div class="record-detail-grid">
                <article class="record-field wide">
                    <span>What It Is Used For</span>
                    <p><?= htmlspecialchars(medicine_used_for_text($medicine), ENT_QUOTES) ?></p>
                    <?php if ($useSourceLinks): ?>
                        <div class="source-links">
                            <?php foreach ($useSourceLinks as $label => $url): ?>
                                <a href="<?= htmlspecialchars((string) $url, ENT_QUOTES) ?>"><?= htmlspecialchars((string) $label, ENT_QUOTES) ?></a>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                </article>
                <article class="record-field">
                    <span>Active Ingredient</span>
                    <p><?= htmlspecialchars((string) ($medicine['active_ingredient'] ?: $medicine['generic_name']), ENT_QUOTES) ?></p>
                </article>
                <article class="record-field">
                    <span>Chemical Name</span>
                    <p><?= htmlspecialchars((string) ($medicine['chemical_name'] ?: $medicine['active_ingredient'] ?: $medicine['generic_name']), ENT_QUOTES) ?></p>
                </article>
                <article class="record-field">
                    <span>Common Brands</span>
                    <p><?= htmlspecialchars((string) ($medicine['brand_names'] ?: 'Brand names vary by market.'), ENT_QUOTES) ?></p>
                </article>
                <article class="record-field">
                    <span>Generic / Alternate Names</span>
                    <p><?= htmlspecialchars((string) ($medicine['generic_brands'] ?: $medicine['generic_name']), ENT_QUOTES) ?></p>
                </article>
                <article class="record-field wide">
                    <span>Tracking Note</span>
                    <p><?= htmlspecialchars((string) ($medicine['tracking_note'] ?: 'Track dose, frequency, route, prescriber, status, side effects, and patient-specific notes.'), ENT_QUOTES) ?></p>
                </article>
                <article class="record-field wide">
                    <span>Reference Links</span>
                    <div class="source-links">
                        <?php foreach ($referenceLinks as $label => $url): ?>
                            <a href="<?= htmlspecialchars((string) $url, ENT_QUOTES) ?>"><?= htmlspecialchars($label, ENT_QUOTES) ?></a>
                        <?php endforeach; ?>
                    </div>
                </article>
            </div>
        </section>

        <?php if ($linkedConditions || $linkedSymptoms): ?>
            <section class="admin-public-shell">
                <div class="site-section-heading">
                    <p class="site-eyebrow">Clinical links</p>
                    <h2>Linked conditions and symptoms</h2>
                </div>
                <div class="library-list">
                    <?php foreach ($linkedConditions as $condition): ?>
                        <article class="library-row">
                            <div class="library-row-main">
                                <h3><?= htmlspecialchars((string) $condition['name'], ENT_QUOTES) ?></h3>
                                <p><?= htmlspecialchars((string) ($condition['link_note'] ?: $condition['overview']), ENT_QUOTES) ?></p>
                                <div class="library-row-meta">
                                    <span><?= htmlspecialchars((string) $condition['category'], ENT_QUOTES) ?></span>
                                    <span><?= htmlspecialchars((string) $condition['link_relationship'], 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 ($linkedSymptoms as $symptom): ?>
                        <article class="library-row">
                            <div class="library-row-main">
                                <h3><?= htmlspecialchars((string) $symptom['name'], ENT_QUOTES) ?></h3>
                                <p><?= htmlspecialchars((string) ($symptom['link_note'] ?: $symptom['plain_language_description']), ENT_QUOTES) ?></p>
                                <div class="library-row-meta">
                                    <span><?= htmlspecialchars((string) $symptom['category'], ENT_QUOTES) ?></span>
                                    <span><?= htmlspecialchars((string) $symptom['link_relationship'], ENT_QUOTES) ?></span>
                                </div>
                            </div>
                            <div class="library-row-actions">
                                <a class="site-secondary" href="<?= htmlspecialchars(symptom_detail_path($symptom), ENT_QUOTES) ?>">View symptom</a>
                            </div>
                        </article>
                    <?php endforeach; ?>
                </div>
            </section>
        <?php endif; ?>

        <?php if ($relatedStudies): ?>
            <section class="admin-public-shell">
                <div class="site-section-heading">
                    <p class="site-eyebrow">Research</p>
                    <h2>Related NIH/NLM study links</h2>
                </div>
                <div class="library-list">
                    <?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['study_type'], ENT_QUOTES) ?></span>
                                </div>
                            </div>
                            <div class="source-links library-row-actions">
                                <a href="<?= htmlspecialchars((string) $study['source_url'], ENT_QUOTES) ?>"><?= htmlspecialchars((string) $study['nih_source'], ENT_QUOTES) ?></a>
                                <a href="<?= htmlspecialchars((string) $study['pubmed_url'], ENT_QUOTES) ?>">PubMed</a>
                                <a href="<?= htmlspecialchars((string) $study['clinicaltrials_url'], ENT_QUOTES) ?>">ClinicalTrials.gov</a>
                            </div>
                        </article>
                    <?php endforeach; ?>
                </div>
            </section>
        <?php endif; ?>
    <?php endif; ?>
</main>
<?php require __DIR__ . '/partials/site-footer.php'; ?>
