<?php
session_start();

require_once __DIR__ . '/../../includes/helpers.php';
require_once __DIR__ . '/../../includes/layout.php';
require_once __DIR__ . '/../../includes/expense_shell.php';

if (!isset($_SESSION['authenticated'])) {
    redirect(page_url('signin'));
}

$userId = current_user_id();
if ($userId <= 0) {
    redirect(page_url('signin'));
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $helpMessage = trim((string) ($_POST['help_message'] ?? ''));
    $contactNumber = trim((string) ($_POST['contact_number'] ?? ''));
    $convenientDateTime = trim((string) ($_POST['convenient_datetime'] ?? ''));

    $errors = [];

    if ($helpMessage === '') {
        $errors[] = 'Please describe how we can help you.';
    }

    if ($contactNumber === '') {
        $errors[] = 'Please enter a contact number.';
    } elseif (!preg_match('/^[0-9+\-\s()]{7,20}$/', $contactNumber)) {
        $errors[] = 'Please enter a valid contact number.';
    }

    if ($convenientDateTime === '') {
        $errors[] = 'Please add a convenient date and time.';
    }

    if ($errors !== []) {
        $_SESSION['old'] = [
            'help_message' => $helpMessage,
            'contact_number' => $contactNumber,
            'convenient_datetime' => $convenientDateTime,
        ];
        flash('support_error', implode(' ', $errors));
        redirect(route_url('support/contact/'));
    }

    $_SESSION['old'] = [];
    flash('support_success', 'Your support request has been submitted. Our team will reach out shortly.');
    redirect(route_url('support/contact/'));
}

$supportSuccess = flash('support_success');
$supportError = flash('support_error');

$formValues = [
    'help_message' => old('help_message'),
    'contact_number' => old('contact_number'),
    'convenient_datetime' => old('convenient_datetime'),
];

render_expense_shell_start('Contact Support | Expense', 'dashboard-page support-page', 'home');
?>
<div class="page-surface support-page-surface">
    <div class="support-page-overlay" aria-hidden="true"></div>

    <section class="support-request-panel panel">
        <header class="support-request-header">
            <div>
                <p class="support-request-kicker">Help &amp; Support</p>
                <h1>Onboarding Call</h1>
            </div>

            <a class="page-close-button" href="<?= e(page_url('dashboard')) ?>" aria-label="Close">
                <i class="fa-solid fa-xmark" aria-hidden="true"></i>
            </a>
        </header>

        <p class="support-request-copy">
            Share what you need help with and pick a convenient time. We'll route the request to the right consultant.
        </p>

        <?php if ($supportSuccess !== null): ?>
            <div class="support-request-flash support-request-flash--success"><?= e($supportSuccess) ?></div>
        <?php endif; ?>

        <?php if ($supportError !== null): ?>
            <div class="support-request-flash support-request-flash--error"><?= e($supportError) ?></div>
        <?php endif; ?>

        <form class="support-request-form" method="post" enctype="multipart/form-data">
            <label class="support-field support-field--required">
                <span>How can we help you? *</span>
                <textarea name="help_message" rows="7" placeholder="Tell us about the issue, setup help, or questions you have."><?= e($formValues['help_message']) ?></textarea>
            </label>

            <label class="support-field">
                <span>Attachments</span>
                <div class="support-upload">
                    <input type="file" name="attachment" accept=".pdf,.png,.jpg,.jpeg,.doc,.docx">
                    <small>Optional. Upload screenshots or a document if that helps explain the request.</small>
                </div>
            </label>

            <label class="support-field support-field--required">
                <span>Contact Number *</span>
                <input
                    type="tel"
                    name="contact_number"
                    inputmode="tel"
                    placeholder="Enter your phone number"
                    value="<?= e($formValues['contact_number']) ?>"
                >
            </label>

            <div class="support-form-grid">
                <label class="support-field support-field--required">
                    <span>Convenient Date &amp; Time *</span>
                    <input
                        type="text"
                        name="convenient_datetime"
                        placeholder="eg: Sep 18, 9:00-11:00 AM PST"
                        value="<?= e($formValues['convenient_datetime']) ?>"
                    >
                </label>
                <p class="support-hint">Example: Sep 18, 9:00-11:00 AM PST</p>
            </div>

            <div class="support-submit-row">
                <button type="submit" class="support-submit">Submit Request</button>
                <a class="support-back-link" href="<?= e(page_url('dashboard')) ?>">Back to Home</a>
            </div>
        </form>
    </section>
</div>
<?php render_expense_shell_end(); ?>
