<?php

declare(strict_types=1);

require __DIR__ . '/app/bootstrap.php';
require __DIR__ . '/app/layout.php';

// Public clinic details must reflect newly saved website settings immediately,
// including behind browsers or shared hosting caches.
if (!headers_sent()) {
    header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
    header('Pragma: no-cache');
}

$clinic = public_clinic_profile();
$landing = public_landing_content();
$sectionStyle = static function (string $key) use ($landing): string { return ' style="order:' . (int) public_landing_section_order($key, $landing) . '"'; };
$sectionImage = static function (string $key) use ($landing): string { return public_landing_section_image($key, $landing); };
$sectionImageAlt = static function (string $key) use ($landing): string { return public_landing_section_image_alt($key, $landing); };
$serviceRows = public_services_catalog(public_request_branch_id());
$services = array_values(array_map(static fn(array $service): string => (string)$service['name'], $serviceRows));
$servicePreview = array_slice($services, 0, 10);
$serviceExtraCount = max(0, count($services) - count($servicePreview));
$distributionProfile = function_exists('dental_distribution_profile') ? dental_distribution_profile() : [];
// Show buyer download access only on the owner/source landing page.
// Distributed client/demo copies keep installer downloads hidden.
$showReleaseDownloads = empty($distributionProfile);
$hours = public_lines((string) $clinic['hours']);
$intakeReady = public_intake_schema_ready();
$publicDentists = [];
if (doctor_workflow_schema_ready()) {
    try {
        $publicDentists = db()->query('SELECT dp.user_id,dp.professional_title,u.name display_name,dp.photo_path,dp.public_biography,dp.languages_json,ds.name specialty_name,b.name branch_name FROM `' . table('dentist_profiles') . '` dp JOIN `' . table('users') . '` u ON u.id=dp.user_id LEFT JOIN `' . table('dentist_specialties') . '` ds ON ds.id=dp.specialty_id LEFT JOIN `' . table('branches') . '` b ON b.id=dp.primary_branch_id WHERE dp.public_profile_enabled=1 AND dp.profile_status="active" AND u.status="active" ORDER BY display_name')->fetchAll();
    } catch (Throwable) { $publicDentists = []; }
}

render_header('Home', true);
render_public_nav('home');
?>
<main>
    <?php if (public_landing_section_visible('hero', $landing)): ?><section class="public-hero"<?= $sectionStyle('hero') ?>>
        <div class="public-hero-copy">
            <span class="eyebrow"><?= e((string) $landing['hero_eyebrow']) ?></span>
            <h1><?= e((string) $landing['hero_title']) ?></h1>
            <p class="hero-tagline"><?= e((string) $clinic['tagline']) ?></p>
            <p><?= e((string) $clinic['intro']) ?></p>
            <div class="hero-actions">
                <a class="btn primary large-button" href="appointment-new.php"><?= e((string) $landing['primary_cta_label']) ?></a>
                <a class="btn ghost large-button" href="appointment-returning.php"><?= e((string) $landing['secondary_cta_label']) ?></a>
            </div>
            <div class="public-trust-row">
                <?php foreach (array_slice(public_lines((string) $landing['trust_items']), 0, 4) as $trustItem): ?><span>✓ <?= e($trustItem) ?></span><?php endforeach; ?>
            </div>
        </div>
        <?php if ($sectionImage('hero')): ?><figure class="public-hero-builder-image"><img src="<?= e($sectionImage('hero')) ?>" alt="<?= e($sectionImageAlt('hero') ?: (string)$clinic['name']) ?>"></figure><?php endif; ?>
        <aside class="clinic-info-card">
            <div class="clinic-info-heading"><span class="mark large">DC</span><div><strong><?= e((string) $landing['clinic_info_heading']) ?></strong><small><?= e((string) $landing['clinic_info_subtitle']) ?></small></div></div>
            <dl class="clinic-info-list">
                <dt><?= e((string) $landing['clinic_info_phone_label']) ?></dt><dd><?= e((string) ($clinic['phone'] ?: 'Contact the clinic')) ?></dd>
                <dt><?= e((string) $landing['clinic_info_email_label']) ?></dt><dd><?= e((string) ($clinic['public_email'] ?: 'Available through the clinic')) ?></dd>
                <dt><?= e((string) $landing['clinic_info_address_label']) ?></dt><dd><?= nl2br(e((string) ($clinic['address'] ?: 'Clinic address will appear here'))) ?></dd>
            </dl>
            <div class="hours-list"><strong><?= e((string) $landing['clinic_info_hours_label']) ?></strong><?php foreach ($hours as $line): ?><span><?= e($line) ?></span><?php endforeach; ?></div>
            <a class="text-link" href="#contact"><?= e((string) $landing['clinic_info_contact_link_label']) ?></a>
        </aside>
    </section><?php endif; ?>

    <?php if (public_landing_section_visible('information', $landing)): ?><section id="information" class="public-section info-band"<?= $sectionStyle('information') ?>>
        <div class="public-section-heading left">
            <span class="eyebrow"><?= e((string) $landing['info_eyebrow']) ?></span>
            <h2><?= e((string) $landing['info_title']) ?></h2>
            <p><?= e((string) $landing['info_body']) ?></p>
        </div>
        <?php if ($sectionImage('information')): ?><figure class="public-section-builder-image"><img src="<?= e($sectionImage('information')) ?>" alt="<?= e($sectionImageAlt('information') ?: (string)$landing['info_title']) ?>"></figure><?php endif; ?>
        <div class="info-points">
            <?php foreach (array_slice(public_lines((string) $landing['info_points']), 0, 6) as $i => $pointLine): $parts = array_map('trim', explode('|', $pointLine, 2)); ?>
            <article><span><?= str_pad((string)($i + 1), 2, '0', STR_PAD_LEFT) ?></span><div><h3><?= e($parts[0] ?: 'Information') ?></h3><p><?= e($parts[1] ?? '') ?></p></div></article>
            <?php endforeach; ?>
        </div>
    </section><?php endif; ?>

    <?php if (public_landing_section_visible('appointment', $landing)): ?><section id="appointment" class="public-section appointment-center"<?= $sectionStyle('appointment') ?>>
        <div class="public-section-heading">
            <span class="eyebrow"><?= e((string) $landing['appointment_eyebrow']) ?></span>
            <h2><?= e((string) $landing['appointment_title']) ?></h2>
            <p><?= e((string) $landing['appointment_intro']) ?></p>
        </div>
        <?php if (!$intakeReady): ?>
            <div class="public-notice warning"><strong>Appointment registration update required.</strong> The Superuser must apply pending updates in <strong>System Setup → Updates &amp; Database</strong> before public requests can be accepted.</div>
        <?php endif; ?>
        <?php if ($sectionImage('appointment')): ?><figure class="public-section-builder-image centered"><img src="<?= e($sectionImage('appointment')) ?>" alt="<?= e($sectionImageAlt('appointment') ?: (string)$landing['appointment_title']) ?>"></figure><?php endif; ?>
        <div class="appointment-choice-grid">
            <article class="appointment-choice featured">
                <span class="choice-label"><?= e((string) $landing['first_visit_label']) ?></span>
                <div class="choice-icon"><?= e((string) $landing['first_visit_icon']) ?></div>
                <h3><?= e((string) $landing['first_visit_title']) ?></h3>
                <p><?= e((string) $landing['first_visit_body']) ?></p>
                <ul><?php foreach (public_lines((string) $landing['first_visit_items']) as $item): ?><li><?= e($item) ?></li><?php endforeach; ?></ul>
                <a class="btn primary" href="appointment-new.php"><?= e((string) $landing['first_visit_cta_label']) ?></a>
            </article>
            <article class="appointment-choice">
                <span class="choice-label"><?= e((string) $landing['returning_visit_label']) ?></span>
                <div class="choice-icon"><?= e((string) $landing['returning_visit_icon']) ?></div>
                <h3><?= e((string) $landing['returning_visit_title']) ?></h3>
                <p><?= e((string) $landing['returning_visit_body']) ?></p>
                <ul><?php foreach (public_lines((string) $landing['returning_visit_items']) as $item): ?><li><?= e($item) ?></li><?php endforeach; ?></ul>
                <a class="btn ghost" href="appointment-returning.php"><?= e((string) $landing['returning_visit_cta_label']) ?></a>
            </article>
        </div>
        <div class="appointment-note"><strong>Please note:</strong> <?= e((string) $clinic['appointment_note']) ?></div>
    </section><?php endif; ?>

    <?php if ($publicDentists && public_landing_section_visible('dentists', $landing)): ?>
    <section class="public-section dentist-section"<?= $sectionStyle('dentists') ?>>
        <div class="public-section-heading"><span class="eyebrow"><?= e((string) $landing['dentists_eyebrow']) ?></span><h2><?= e((string) $landing['dentists_title']) ?></h2><p><?= e((string) $landing['dentists_intro']) ?></p></div>
        <?php if ($sectionImage('dentists')): ?><figure class="public-section-builder-image centered"><img src="<?= e($sectionImage('dentists')) ?>" alt="<?= e($sectionImageAlt('dentists') ?: 'Clinic team') ?>"></figure><?php endif; ?>
        <div class="public-dentist-grid">
            <?php foreach ($publicDentists as $doctor): $langs=json_decode((string)($doctor['languages_json']??'[]'),true); if(!is_array($langs))$langs=[]; ?>
            <article class="public-dentist-card">
                <div class="public-dentist-photo"><?php if($doctor['photo_path']):?><img src="doctor_asset.php?type=photo&variant=portrait&user_id=<?=(int)$doctor['user_id']?>" alt="<?=e($doctor['display_name'])?>"><?php else:?><span><?=e(strtoupper(substr((string)$doctor['display_name'],0,1)))?></span><?php endif;?></div>
                <div><h3><?=e(trim(($doctor['professional_title']?:'').' '.$doctor['display_name']))?></h3><p><strong><?=e($doctor['specialty_name']?:'General Dentistry')?></strong><?= $doctor['branch_name']?' · '.e($doctor['branch_name']):'' ?></p><?php if($doctor['public_biography']):?><p><?=e($doctor['public_biography'])?></p><?php endif;?><?php if($langs):?><small>Languages: <?=e(implode(', ',$langs))?></small><?php endif;?></div>
            </article>
            <?php endforeach; ?>
        </div>
    </section>
    <?php endif; ?>

    <?php if (public_landing_section_visible('services', $landing)): ?><section id="services" class="public-section services-section compact-services-section"<?= $sectionStyle('services') ?>>
        <div class="public-section-heading">
            <span class="eyebrow"><?= e((string) $landing['services_eyebrow']) ?></span>
            <h2><?= e((string) $landing['services_title']) ?></h2>
            <p><?= e((string) $landing['services_intro']) ?></p>
        </div>
        <?php if ($sectionImage('services')): ?><figure class="public-section-builder-image centered"><img src="<?= e($sectionImage('services')) ?>" alt="<?= e($sectionImageAlt('services') ?: (string)$landing['services_title']) ?>"></figure><?php endif; ?>
        <div class="service-chip-grid">
            <?php foreach ($servicePreview as $service): ?>
                <span><?= e($service) ?></span>
            <?php endforeach; ?>
            <?php if ($serviceExtraCount > 0): ?><span class="service-chip-muted"><?= e(sprintf((string)$landing['services_more_label'], (int)$serviceExtraCount)) ?></span><?php endif; ?>
        </div>
        <div class="service-cta-row"><a class="btn ghost" href="appointment-new.php"><?= e((string) $landing['services_primary_cta_label']) ?></a><a class="btn ghost" href="appointment-returning.php"><?= e((string) $landing['services_secondary_cta_label']) ?></a></div>
    </section><?php endif; ?>

    <?php if (public_landing_section_visible('care_flow', $landing)): ?><section class="public-section care-flow-section"<?= $sectionStyle('care_flow') ?>>
        <div class="public-section-heading">
            <span class="eyebrow"><?= e((string) $landing['care_flow_eyebrow']) ?></span>
            <h2><?= e((string) $landing['care_flow_title']) ?></h2>
        </div>
        <?php if ($sectionImage('care_flow')): ?><figure class="public-section-builder-image centered"><img src="<?= e($sectionImage('care_flow')) ?>" alt="<?= e($sectionImageAlt('care_flow') ?: (string)$landing['care_flow_title']) ?>"></figure><?php endif; ?>
        <div class="public-workflow">
            <?php foreach (public_lines((string) $landing['care_flow_steps']) as $number => $step): ?>
                <div><span><?= $number + 1 ?></span><strong><?= e($step) ?></strong></div>
            <?php endforeach; ?>
        </div>
    </section><?php endif; ?>

    <?php if (public_landing_section_visible('contact', $landing)): ?><section id="contact" class="public-section contact-section"<?= $sectionStyle('contact') ?>>
        <div>
            <span class="eyebrow"><?= e((string) $landing['contact_eyebrow']) ?></span>
            <h2><?= e((string) $landing['contact_title']) ?></h2>
            <p><?= e((string) $landing['contact_body']) ?></p>
            <div class="contact-actions">
                <?php if ($clinic['phone']): ?><a class="btn primary" href="tel:<?= e(preg_replace('/[^0-9+]/', '', (string) $clinic['phone'])) ?>"><?= e(str_replace('{phone}', (string)$clinic['phone'], (string)$landing['contact_call_label'])) ?></a><?php endif; ?>
                <?php if ($clinic['public_email']): ?><a class="btn ghost" href="mailto:<?= e((string) $clinic['public_email']) ?>"><?= e((string) $landing['contact_email_label']) ?></a><?php endif; ?>
            </div>
        </div>
        <?php if ($sectionImage('contact')): ?><figure class="public-section-builder-image contact-builder-image"><img src="<?= e($sectionImage('contact')) ?>" alt="<?= e($sectionImageAlt('contact') ?: (string)$landing['contact_title']) ?>"></figure><?php endif; ?>
        <div class="contact-card">
            <h3><?= e((string) $clinic['name']) ?></h3>
            <p><?= nl2br(e((string) ($clinic['address'] ?: 'Clinic address will appear here.'))) ?></p>
            <p><strong><?= e((string) $landing['contact_card_phone_label']) ?>:</strong> <?= e((string) ($clinic['phone'] ?: 'Not yet published')) ?><br><strong><?= e((string) $landing['contact_card_email_label']) ?>:</strong> <?= e((string) ($clinic['public_email'] ?: 'Not yet published')) ?></p>
            <div class="hours-list"><strong><?= e((string) $landing['contact_card_hours_label']) ?></strong><?php foreach ($hours as $line): ?><span><?= e($line) ?></span><?php endforeach; ?></div>
        </div>
    </section><?php endif; ?>

    <?php if ($showReleaseDownloads && public_landing_section_visible('downloads', $landing)): ?>
    <section id="downloads" class="public-section release-download-section"<?= $sectionStyle('downloads') ?>>
        <div class="public-section-heading left">
            <span class="eyebrow"><?= e((string) $landing['downloads_eyebrow']) ?></span>
            <h2><?= e((string) $landing['downloads_title']) ?></h2>
            <p><?= e((string) $landing['downloads_intro']) ?></p>
        </div>
        <?php if ($sectionImage('downloads')): ?><figure class="public-section-builder-image centered"><img src="<?= e($sectionImage('downloads')) ?>" alt="<?= e($sectionImageAlt('downloads') ?: 'Buyer installer access') ?>"></figure><?php endif; ?>
        <div class="release-download-grid">
            <article>
                <h3><?= e((string) $landing['download_full_title']) ?></h3>
                <p><?= e((string) $landing['download_full_body']) ?></p>
                <a class="btn primary" href="download_release.php?type=full"><?= e((string) $landing['download_full_cta_label']) ?></a>
            </article>
            <article>
                <h3><?= e((string) $landing['download_demo_title']) ?></h3>
                <p><?= e((string) $landing['download_demo_body']) ?></p>
                <a class="btn ghost" href="download_release.php?type=demo"><?= e((string) $landing['download_demo_cta_label']) ?></a>
            </article>
            <article>
                <h3><?= e((string) $landing['download_support_title']) ?></h3>
                <p><?= e((string) $landing['download_support_body']) ?></p>
                <a class="btn ghost" href="mailto:mtbcustomprints@gmail.com?subject=DentalCare%20Full%20Installer%20Purchase"><?= e((string) $landing['download_support_cta_label']) ?></a>
            </article>
        </div>
    </section>
    <?php endif; ?>

    <?php if (public_landing_section_visible('developer', $landing)): ?><section id="developer" class="public-section developer-section"<?= $sectionStyle('developer') ?>>
        <div>
            <span class="eyebrow"><?= e((string) $landing['developer_eyebrow']) ?></span>
            <h2><?= e((string) $landing['developer_title']) ?></h2>
            <p><?= e((string) $landing['developer_body']) ?></p>
        </div>
        <?php if ($sectionImage('developer')): ?><figure class="public-section-builder-image contact-builder-image"><img src="<?= e($sectionImage('developer')) ?>" alt="<?= e($sectionImageAlt('developer') ?: 'Developer') ?>"></figure><?php endif; ?>
        <div class="developer-card">
            <strong><?= e((string) $landing['developer_card_label']) ?></strong>
            <h3><?= e((string) $landing['developer_name']) ?></h3>
            <p><a href="mailto:<?= e((string) $landing['developer_email']) ?>"><?= e((string) $landing['developer_email']) ?></a></p>
            <p class="muted"><?= e((string) $landing['developer_card_body']) ?></p>
        </div>
    </section><?php endif; ?>

    <?php if (public_landing_section_visible('portal', $landing)): ?><section class="secure-portal-banner"<?= $sectionStyle('portal') ?>>
        <div><span class="eyebrow light-eyebrow"><?= e((string) $landing['portal_eyebrow']) ?></span><h2><?= e((string) $landing['portal_title']) ?></h2><p><?= e((string) $landing['portal_body']) ?></p></div>
        <?php if ($sectionImage('portal')): ?><figure class="public-section-builder-image portal-builder-image"><img src="<?= e($sectionImage('portal')) ?>" alt="<?= e($sectionImageAlt('portal') ?: (string)$landing['portal_title']) ?>"></figure><?php endif; ?>
        <div class="hero-actions">
            <a class="btn light" href="login.php"><?= e((string) $landing['portal_staff_cta_label']) ?></a>
            <a class="btn ghost" href="patient-login.php"><?= e((string) $landing['portal_patient_cta_label']) ?></a>
        </div>
    </section><?php endif; ?>
</main>
<?php
render_public_footer();
render_footer(true);
