<?php
declare(strict_types=1);

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

$pageTitle = 'Medical Reference Library | Medical Tracker';
$activePage = 'home';
$totalMedicines = (int) db_value($pdo, 'SELECT COUNT(*) FROM medication_catalog');
$totalTerms = (int) db_value($pdo, 'SELECT COUNT(*) FROM medical_terms');
$totalSymptoms = (int) db_value($pdo, 'SELECT COUNT(*) FROM medical_symptoms');
$totalConditions = (int) db_value($pdo, 'SELECT COUNT(*) FROM medical_conditions');
$totalStudies = (int) db_value($pdo, 'SELECT COUNT(*) FROM research_studies');

require __DIR__ . '/partials/site-header.php';
?>
<main>
    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">Medical reference</p>
            <h1>Medicines, symptoms, terminology, conditions, and NIH/NLM study links.</h1>
            <p>Search the reference database or open the tracker/admin tools for profile-managed medical logs.</p>
        </div>

        <form class="library-search" method="get" action="/medical.php">
            <label>
                <span>Search medical library</span>
                <input name="q" placeholder="Try warfarin, CBC, blood pressure, metformin, asthma">
            </label>
            <button class="site-primary" type="submit">Search</button>
        </form>

        <div class="library-stats" aria-label="Library totals">
            <article><strong><?= number_format($totalMedicines) ?></strong><span>medicines</span></article>
            <article><strong><?= number_format($totalSymptoms) ?></strong><span>symptoms</span></article>
            <article><strong><?= number_format($totalTerms) ?></strong><span>terms</span></article>
            <article><strong><?= number_format($totalConditions) ?></strong><span>conditions</span></article>
            <article><strong><?= number_format($totalStudies) ?></strong><span>study categories</span></article>
        </div>
    </section>

    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">Reference sections</p>
            <h2>Open a database area.</h2>
        </div>
        <div class="feature-grid">
            <article>
                <span>Rx</span>
                <h3>Medicines</h3>
                <p>Search by generic name, chemical name, active ingredient, brand name, or medication class.</p>
                <a class="site-secondary" href="/nutrition.php">Open medicines</a>
            </article>
            <article>
                <span>ABC</span>
                <h3>Terminology</h3>
                <p>Browse plain-language medical terms with clinical context and reference links.</p>
                <a class="site-secondary" href="/medical.php">Open terminology</a>
            </article>
            <article>
                <span>Sx</span>
                <h3>Symptoms</h3>
                <p>Search symptom names, body systems, tracking notes, and urgency-context references.</p>
                <a class="site-secondary" href="/symptoms.php">Open symptoms</a>
            </article>
            <article>
                <span>NIH</span>
                <h3>Research</h3>
                <p>Open NIH/NLM, PubMed, and ClinicalTrials.gov study categories tied to medicines.</p>
                <a class="site-secondary" href="/recovery.php">Open research</a>
            </article>
            <article>
                <span>Dx</span>
                <h3>Conditions</h3>
                <p>Review condition-tracking topics for vitals, labs, symptoms, medications, and notes.</p>
                <a class="site-secondary" href="/workouts.php">Open conditions</a>
            </article>
        </div>
    </section>

    <section class="admin-public-shell">
        <div class="site-section-heading">
            <p class="site-eyebrow">Profile tools</p>
            <h2>Manage medical logs through Tracker and Admin.</h2>
        </div>
        <div class="library-list">
            <article class="library-row">
                <div class="library-row-main">
                    <h3>Tracker</h3>
                    <p>Use existing profiles for medications, vitals, labs, symptoms, allergies, appointments, immunizations, and notes.</p>
                </div>
                <div class="library-row-actions">
                    <a class="site-primary" href="/tracker.php">Open tracker</a>
                </div>
            </article>
            <article class="library-row">
                <div class="library-row-main">
                    <h3>Admin</h3>
                    <p>Create, update, activate, deactivate, and remove profiles from the admin dashboard.</p>
                </div>
                <div class="library-row-actions">
                    <a class="site-secondary" href="/admin.php">Manage profiles</a>
                </div>
            </article>
        </div>
    </section>
</main>
<?php require __DIR__ . '/partials/site-footer.php'; ?>
