<?php

declare(strict_types=1);

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

$clinic = public_clinic_profile();
$branchId = public_request_branch_id();
$serviceRows = public_services_catalog($branchId);
$dentists = active_dentists($branchId);
$errors = [];
$old = $_POST;
$startedAt = time();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $startedAt = (int) ($_POST['form_started'] ?? time());
    try {
        if (!public_intake_schema_ready()) {
            throw new RuntimeException('Online appointment registration is not ready. Please contact the clinic.');
        }
        verify_public_form('new');

        $required = ['first_name', 'last_name', 'birth_date', 'mobile', 'service_id', 'preferred_date', 'preferred_time', 'reason'];
        foreach ($required as $field) {
            if (trim((string) ($_POST[$field] ?? '')) === '') {
                $errors[$field] = 'This field is required.';
            }
        }

        $email = trim((string) ($_POST['email'] ?? ''));
        if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $errors['email'] = 'Enter a valid email address.';
        }

        $birthDate = (string) ($_POST['birth_date'] ?? '');
        $birthTimestamp = strtotime($birthDate);
        if ($birthTimestamp === false || $birthTimestamp > strtotime('today')) {
            $errors['birth_date'] = 'Enter a valid birth date.';
        }
        $age = public_patient_age($birthDate);
        if ($age !== null && $age < 18) {
            if (trim((string) ($_POST['guardian_name'] ?? '')) === '') {
                $errors['guardian_name'] = 'A parent or guardian is required for patients under 18.';
            }
            if (trim((string) ($_POST['guardian_mobile'] ?? '')) === '') {
                $errors['guardian_mobile'] = 'Enter the guardian’s mobile number.';
            }
        }

        $preferredTime = (string) ($_POST['preferred_time'] ?? '');
        if (!preg_match('/^(?:[01]\d|2[0-3]):[0-5]\d$/', $preferredTime)) {
            $errors['preferred_time'] = 'Choose a valid preferred time.';
        }
        $alternateTime = trim((string) ($_POST['alternate_time'] ?? ''));
        if ($alternateTime !== '' && !preg_match('/^(?:[01]\d|2[0-3]):[0-5]\d$/', $alternateTime)) {
            $errors['alternate_time'] = 'Choose a valid alternate time.';
        }

        $preferredDate = (string) ($_POST['preferred_date'] ?? '');
        $preferredTimestamp = strtotime($preferredDate);
        if ($preferredTimestamp === false || $preferredTimestamp < strtotime('today') || $preferredTimestamp > strtotime('+1 year')) {
            $errors['preferred_date'] = 'Choose a date from today up to one year ahead.';
        }

        $alternateDate = trim((string) ($_POST['alternate_date'] ?? ''));
        if ($alternateDate !== '' && (strtotime($alternateDate) === false || strtotime($alternateDate) < strtotime('today'))) {
            $errors['alternate_date'] = 'Choose a valid alternate date.';
        }

        $serviceId = (int) ($_POST['service_id'] ?? 0);
        $doctorId = (int) ($_POST['preferred_dentist_user_id'] ?? 0);
        $providerPreference = (string)($_POST['provider_preference_mode'] ?? 'specific');
        if (!in_array($providerPreference, ['specific','clinic_decides'], true)) $providerPreference = 'specific';
        $serviceRow = public_service_definition($serviceId, $branchId);
        $providerType = $serviceRow ? service_provider_type($serviceRow) : 'dentist';
        $doctorRow = null;
        $availableAtRequestedTime = [];
        if (!$serviceRow) {
            $errors['service_id'] = 'Choose an available service.';
        } elseif ($providerType === 'inhouse') {
            $providerPreference = 'clinic_decides';
            $doctorId = 0;
            if (!$errors && !validate_service_slot((int)$branchId,$serviceRow,$preferredDate,$preferredTime,0)) {
                $errors['preferred_time'] = 'This time is no longer available. Choose another listed time.';
            }
        } else {
            $bookableDentists = bookable_dentists_for_service((int)$branchId,$serviceId,true);
            if ($providerPreference === 'specific') {
                foreach ($bookableDentists as $candidate) {
                    if ((int)$candidate['id'] === $doctorId) { $doctorRow = $candidate; break; }
                }
                if (!$doctorRow) {
                    $errors['preferred_dentist_user_id'] = 'Choose a dentist who is available at the selected time.';
                } elseif (!$errors && !validate_service_slot((int)$branchId,$serviceRow,$preferredDate,$preferredTime,$doctorId)) {
                    $errors['preferred_time'] = 'This dentist is no longer available at that time. Choose another listed option.';
                }
            } else {
                $doctorId = 0;
                if (!$errors) {
                    $availableAtRequestedTime = available_doctors_for_slot((int)$branchId,$serviceId,$preferredDate,$preferredTime,true);
                    if (!$availableAtRequestedTime) {
                        $errors['preferred_time'] = 'No dentist accepting new patients is available at that time. Choose another listed time.';
                    }
                }
            }
        }
        $service = (string) ($serviceRow['name'] ?? '');
        $dentistName = $providerType === 'inhouse'
            ? 'In-house clinic team'
            : ($providerPreference === 'clinic_decides' ? 'Let the dental clinic decide' : (string)($doctorRow['name'] ?? ''));

        $urgency = in_array((string)($_POST['urgency'] ?? ''), ['routine','soon','urgent'], true) ? (string)$_POST['urgency'] : 'routine';
        if ($urgency === 'urgent' && mb_strlen(trim((string)($_POST['reason'] ?? ''))) < 20) {
            $errors['reason'] = 'Describe the urgent concern in enough detail for the clinic to prioritize it. For emergencies, contact the clinic directly.';
        }

        if (!isset($_POST['privacy_consent'])) {
            $errors['privacy_consent'] = 'Consent is required to submit the request.';
        }

        if ($errors) {
            throw new RuntimeException('Review the highlighted fields.');
        }

        $pdo = db();
        $duplicate = $pdo->prepare(
            'SELECT id FROM `' . table('patients') . '` WHERE deleted_at IS NULL AND branch_id=? '
            . 'AND LOWER(first_name)=LOWER(?) AND LOWER(last_name)=LOWER(?) AND birth_date=? LIMIT 1'
        );
        $duplicate->execute([
            (int)$branchId,
            trim((string) $_POST['first_name']),
            trim((string) $_POST['last_name']),
            $birthDate,
        ]);
        $possiblePatientId = $duplicate->fetchColumn();

        $reference = public_reference();
        $request = [
            'public_reference' => $reference,
            'branch_id' => $branchId,
            'patient_type' => 'new',
            'patient_id' => $possiblePatientId !== false ? (int) $possiblePatientId : null,
            'first_name' => trim((string) $_POST['first_name']),
            'middle_name' => trim((string) ($_POST['middle_name'] ?? '')) ?: null,
            'last_name' => trim((string) $_POST['last_name']),
            'birth_date' => $birthDate,
            'sex' => trim((string) ($_POST['sex'] ?? '')) ?: null,
            'mobile' => trim((string) $_POST['mobile']),
            'email' => $email ?: null,
            'address' => trim((string) ($_POST['address'] ?? '')) ?: null,
            'emergency_contact' => trim((string) ($_POST['emergency_contact'] ?? '')) ?: null,
            'guardian_name' => trim((string) ($_POST['guardian_name'] ?? '')) ?: null,
            'guardian_mobile' => trim((string) ($_POST['guardian_mobile'] ?? '')) ?: null,
            'service_name' => $service,
            'service_id' => $serviceId,
            'preferred_dentist' => $dentistName,
            'preferred_dentist_user_id' => $doctorId > 0 ? $doctorId : null,
            'provider_preference_mode' => $providerPreference,
            'preferred_date' => $preferredDate,
            'preferred_time' => $preferredTime,
            'alternate_date' => $alternateDate ?: null,
            'alternate_time' => $alternateTime ?: null,
            'urgency' => $urgency,
            'reason' => trim((string) $_POST['reason']),
            'allergies' => trim((string) ($_POST['allergies'] ?? '')) ?: null,
            'medications' => trim((string) ($_POST['medications'] ?? '')) ?: null,
            'conditions' => trim((string) ($_POST['conditions'] ?? '')) ?: null,
            'status' => $possiblePatientId !== false ? 'needs_verification' : 'submitted',
            'submitted_ip' => substr((string) ($_SERVER['REMOTE_ADDR'] ?? ''), 0, 45) ?: null,
        ];

        $providerPreferenceReady = schema_column_exists(table('appointment_requests'),'provider_preference_mode');
        $providerPreferenceColumn = $providerPreferenceReady ? ',provider_preference_mode' : '';
        $providerPreferenceValue = $providerPreferenceReady ? ',:provider_preference_mode' : '';
        if (!$providerPreferenceReady) unset($request['provider_preference_mode']);
        $stmt = $pdo->prepare(
            'INSERT INTO `' . table('appointment_requests') . '` '
            . '(public_reference,branch_id,patient_type,patient_id,first_name,middle_name,last_name,birth_date,sex,mobile,email,address,emergency_contact,guardian_name,guardian_mobile,service_name,service_id,preferred_dentist,preferred_dentist_user_id' . $providerPreferenceColumn . ',preferred_date,preferred_time,alternate_date,alternate_time,urgency,reason,allergies,medications,conditions,status,consent_at,submitted_ip,created_at) '
            . 'VALUES (:public_reference,:branch_id,:patient_type,:patient_id,:first_name,:middle_name,:last_name,:birth_date,:sex,:mobile,:email,:address,:emergency_contact,:guardian_name,:guardian_mobile,:service_name,:service_id,:preferred_dentist,:preferred_dentist_user_id' . $providerPreferenceValue . ',:preferred_date,:preferred_time,:alternate_date,:alternate_time,:urgency,:reason,:allergies,:medications,:conditions,:status,NOW(),:submitted_ip,NOW())'
        );
        $stmt->execute($request);
        $requestId=(int)$pdo->lastInsertId();
        $_SESSION['last_public_appointment_submission'] = time();
        $_SESSION['public_request_receipt'] = [
            'reference' => $reference,
            'name' => $request['first_name'],
            'service' => $request['service_name'],
            'preferred_date' => $request['preferred_date'],
            'preferred_time' => $request['preferred_time'],
        ];
        public_request_email((string) ($request['email'] ?? ''), (string) $request['first_name'], $reference, $request, $clinic,$requestId);
        clinic_request_notification($reference, $request, $clinic);
        header('Location: appointment-received.php');
        exit;
    } catch (Throwable $exception) {
        if (!$errors) {
            $errors['form'] = $exception->getMessage();
        }
    }
}

render_header('New Patient Appointment', true);
render_public_nav('appointment');
?>
<main class="public-form-page">
    <section class="public-form-intro">
        <span class="eyebrow">New patient registration</span>
        <h1>Request your first appointment</h1>
        <p>Tell the clinic who you are, why you need a dental visit, and which schedule works best. The clinic will confirm the final appointment after reviewing availability.</p>
        <div class="form-step-line"><span class="active">1. Patient details</span><span>2. Medical information</span><span>3. Appointment preference</span></div>
    </section>

    <?php if (!public_intake_schema_ready()): ?>
        <div class="public-notice warning"><strong>Online registration is temporarily unavailable.</strong> Please contact the clinic while the administrator completes the appointment-intake update.</div>
    <?php endif; ?>
    <?php if (isset($errors['form'])): ?><div class="public-notice error"><?= e($errors['form']) ?></div><?php endif; ?>

    <form method="post" class="public-intake-form" novalidate>
        <input type="hidden" name="csrf" value="<?= e(csrf_token()) ?>">
        <input type="hidden" name="form_started" value="<?= e((string) $startedAt) ?>">
        <input type="hidden" name="form_signature" value="<?= e(public_form_signature('new', $startedAt)) ?>">
        <label class="honeypot" aria-hidden="true">Website<input name="website" tabindex="-1" autocomplete="off"></label>

        <section class="form-section-card">
            <div class="form-section-heading"><span>01</span><div><h2>Patient information</h2><p>Enter the information that will identify the patient record.</p></div></div>
            <div class="form-grid three-columns">
                <label>First name<input name="first_name" maxlength="100" value="<?= e((string) ($old['first_name'] ?? '')) ?>" required><?php if (isset($errors['first_name'])): ?><small class="field-error"><?= e($errors['first_name']) ?></small><?php endif; ?></label>
                <label>Middle name<input name="middle_name" maxlength="100" value="<?= e((string) ($old['middle_name'] ?? '')) ?>"></label>
                <label>Last name<input name="last_name" maxlength="100" value="<?= e((string) ($old['last_name'] ?? '')) ?>" required><?php if (isset($errors['last_name'])): ?><small class="field-error"><?= e($errors['last_name']) ?></small><?php endif; ?></label>
                <label>Birth date<input type="date" name="birth_date" max="<?= e(date('Y-m-d')) ?>" value="<?= e((string) ($old['birth_date'] ?? '')) ?>" data-birth-date required><?php if (isset($errors['birth_date'])): ?><small class="field-error"><?= e($errors['birth_date']) ?></small><?php endif; ?></label>
                <label>Sex<select name="sex"><option value="">Select</option><?php foreach (['Female', 'Male', 'Other', 'Prefer not to say'] as $option): ?><option value="<?= e($option) ?>" <?= (($old['sex'] ?? '') === $option) ? 'selected' : '' ?>><?= e($option) ?></option><?php endforeach; ?></select></label>
                <label>Mobile number<input name="mobile" maxlength="40" value="<?= e((string) ($old['mobile'] ?? '')) ?>" required><?php if (isset($errors['mobile'])): ?><small class="field-error"><?= e($errors['mobile']) ?></small><?php endif; ?></label>
                <label>Email address<input type="email" name="email" maxlength="190" value="<?= e((string) ($old['email'] ?? '')) ?>"><?php if (isset($errors['email'])): ?><small class="field-error"><?= e($errors['email']) ?></small><?php endif; ?></label>
                <label class="span-2">Home address<textarea name="address" maxlength="1000"><?= e((string) ($old['address'] ?? '')) ?></textarea></label>
                <label>Emergency contact<input name="emergency_contact" maxlength="190" placeholder="Name, relationship, and mobile" value="<?= e((string) ($old['emergency_contact'] ?? '')) ?>"></label>
            </div>
            <div class="guardian-fields" data-guardian-fields>
                <div class="minor-note">Parent or guardian information is required for patients under 18.</div>
                <div class="form-grid">
                    <label>Parent or guardian name<input name="guardian_name" maxlength="190" value="<?= e((string) ($old['guardian_name'] ?? '')) ?>"><?php if (isset($errors['guardian_name'])): ?><small class="field-error"><?= e($errors['guardian_name']) ?></small><?php endif; ?></label>
                    <label>Guardian mobile number<input name="guardian_mobile" maxlength="40" value="<?= e((string) ($old['guardian_mobile'] ?? '')) ?>"><?php if (isset($errors['guardian_mobile'])): ?><small class="field-error"><?= e($errors['guardian_mobile']) ?></small><?php endif; ?></label>
                </div>
            </div>
        </section>

        <section class="form-section-card">
            <div class="form-section-heading"><span>02</span><div><h2>Medical and dental information</h2><p>Share information that may affect safe dental care.</p></div></div>
            <div class="form-grid">
                <label>Known allergies<textarea name="allergies" placeholder="Medication, latex, food, or other allergies"><?= e((string) ($old['allergies'] ?? '')) ?></textarea></label>
                <label>Current medications<textarea name="medications" placeholder="Include medication name and purpose when known"><?= e((string) ($old['medications'] ?? '')) ?></textarea></label>
                <label class="wide">Health information relevant to dental care<textarea name="conditions" placeholder="Examples: hypertension, diabetes, heart condition, pregnancy, bleeding disorder, anticoagulants, or previous anesthesia reaction"><?= e((string) ($old['conditions'] ?? '')) ?></textarea></label>
            </div>
        </section>

        <section class="form-section-card">
            <div class="form-section-heading"><span>03</span><div><h2>Appointment preference</h2><p>Select your preferred visit. The clinic may offer another time if the slot is unavailable.</p></div></div>
            <div class="form-grid three-columns">
                <div class="wide form-grid three-columns" data-slot-picker data-schedule-first data-provider-choice-enabled data-patient-type="new" data-branch-id="<?= e((string) $branchId) ?>" data-preferred-time="<?=e((string)($old['preferred_time']??''))?>" data-preferred-doctor="<?=e((string)($old['preferred_dentist_user_id']??''))?>">
                    <label>Service needed<select name="service_id" data-slot-service required><option value="">Choose a service</option><?php foreach ($serviceRows as $service): ?><option value="<?= e((string)$service['id']) ?>" data-provider-type="<?=e(service_provider_type($service))?>" data-requires-chair="<?=(int)($service['requires_chair']??1)?>" <?= ((int)($old['service_id'] ?? 0) === (int)$service['id']) ? 'selected' : '' ?>><?= e((string)$service['name']) ?> · <?= e((string)$service['duration_minutes']) ?> min</option><?php endforeach; ?></select><?php if (isset($errors['service_id'])): ?><small class="field-error"><?= e($errors['service_id']) ?></small><?php endif; ?></label>
                    <fieldset class="wide provider-choice" data-provider-choice><legend>Who should provide the dental care?</legend><label class="check-label"><input type="radio" name="provider_preference_mode" value="specific" <?= (($old['provider_preference_mode'] ?? 'specific') === 'specific') ? 'checked' : '' ?>><span><strong>Choose an available dentist</strong><small>After you select a time, the system will show only dentists accepting new patients who can provide the selected service.</small></span></label><label class="check-label"><input type="radio" name="provider_preference_mode" value="clinic_decides" <?= (($old['provider_preference_mode'] ?? '') === 'clinic_decides') ? 'checked' : '' ?>><span><strong>Let the dental clinic decide</strong><small>The clinic will assign an appropriate available dentist when staff confirms the request.</small></span></label></fieldset>
                    <label>Preferred date<input type="date" name="preferred_date" data-slot-date min="<?= e(date('Y-m-d')) ?>" max="<?= e(date('Y-m-d', strtotime('+1 year'))) ?>" value="<?= e((string) ($old['preferred_date'] ?? '')) ?>" required><?php if (isset($errors['preferred_date'])): ?><small class="field-error"><?= e($errors['preferred_date']) ?></small><?php endif; ?></label>
                    <div class="available-dates-panel" data-available-dates>Choose a service to see the next available dates and times.</div>
                    <label>Preferred available time<select name="preferred_time" data-slot-time required><option value="">Select service and date</option></select><?php if (isset($errors['preferred_time'])): ?><small class="field-error"><?= e($errors['preferred_time']) ?></small><?php endif; ?></label>
                    <label data-slot-doctor-label>Available dentist<select name="preferred_dentist_user_id" data-slot-doctor><option value="">Choose a time first</option></select><?php if (isset($errors['preferred_dentist_user_id'])): ?><small class="field-error"><?= e($errors['preferred_dentist_user_id']) ?></small><?php endif; ?></label>
                    <div class="slot-status span-2" data-slot-status>Choose the preferred date and time first. DentalCare will then show only doctors who can accept that service at the selected schedule.</div>
                </div>
                <label>Urgency<select name="urgency" data-urgency-select><option value="routine" <?= (($old['urgency'] ?? 'routine') === 'routine') ? 'selected' : '' ?>>Routine</option><option value="soon" <?= (($old['urgency'] ?? '') === 'soon') ? 'selected' : '' ?>>Need an appointment soon</option><option value="urgent" <?= (($old['urgency'] ?? '') === 'urgent') ? 'selected' : '' ?>>Urgent dental concern</option></select></label>
                <div class="public-notice warning wide" data-urgent-notice hidden><strong>Urgent concern:</strong> This form does not provide emergency care. Contact the clinic directly for severe pain, uncontrolled bleeding, facial swelling, breathing difficulty, or trauma.</div>
                <span></span><span></span>
                <label>Alternate date<input type="date" name="alternate_date" min="<?= e(date('Y-m-d')) ?>" max="<?= e(date('Y-m-d', strtotime('+1 year'))) ?>" value="<?= e((string) ($old['alternate_date'] ?? '')) ?>"><?php if (isset($errors['alternate_date'])): ?><small class="field-error"><?= e($errors['alternate_date']) ?></small><?php endif; ?></label>
                <label>Alternate time<input type="time" name="alternate_time" value="<?= e((string) ($old['alternate_time'] ?? '')) ?>"></label>
                <span></span>
                <label class="wide">Reason for visit or main dental concern<textarea name="reason" maxlength="2000" required placeholder="Describe pain, sensitivity, swelling, broken tooth, routine cleaning, or another concern."><?= e((string) ($old['reason'] ?? '')) ?></textarea><?php if (isset($errors['reason'])): ?><small class="field-error"><?= e($errors['reason']) ?></small><?php endif; ?></label>
                <p class="wide muted"><strong>Schedule notice:</strong> This is a preferred time only. It is not reserved until the clinic confirms the appointment.</p>
            </div>
        </section>

        <section class="form-consent-card">
            <label class="check-label"><input type="checkbox" name="privacy_consent" value="1" <?= isset($old['privacy_consent']) ? 'checked' : '' ?>><span>I confirm that the information is accurate and authorize the clinic to use it to review this appointment request and prepare for dental care. I understand that this request is not a confirmed appointment.</span></label>
            <?php if (isset($errors['privacy_consent'])): ?><small class="field-error"><?= e($errors['privacy_consent']) ?></small><?php endif; ?>
            <div class="form-submit-row"><a class="btn ghost" href="index.php#appointment">Back</a><button class="btn primary" type="submit" <?= !public_intake_schema_ready() ? 'disabled' : '' ?>>Submit Appointment Request</button></div>
        </section>
    </form>
</main>
<?php
render_public_footer();
render_footer(true);
