<?php

declare(strict_types=1);

require_once __DIR__ . '/core/bootstrap.php';

HyroCsrf::startSession();

$countries = require __DIR__ . '/config/countries.php';

$allowedProducts = [
    'SPA' => 'HYRO ONE SPA',
    'GYM' => 'HYRO ONE Gym',
    'ACCOUNTING' => 'HYRO ONE Accounting',
    'RETAIL' => 'HYRO ONE Retail',
    'CRM' => 'HYRO ONE CRM',
    'SCHOOL' => 'HYRO ONE School',
];

$selectedProduct = strtoupper(
    trim((string) ($_GET['product'] ?? $_POST['product_code'] ?? 'SPA'))
);

if (!isset($allowedProducts[$selectedProduct])) {
    $selectedProduct = 'SPA';
}

$errors = [];
$old = $_POST;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        if (!HyroCsrf::verify($_POST['_token'] ?? null)) {
            throw new RuntimeException(
                'Your session expired. Please refresh the page and try again.'
            );
        }

        if (
            trim((string) ($_POST['website'] ?? '')) !== ''
        ) {
            hyro_redirect(
                '/experience/demo-thank-you.php'
            );
        }

        $repository = new HyroLeadRepository(
            HyroDatabase::connection()
        );

        $service = new HyroLeadService(
            $repository,
            $countries
        );

        $errors = $service->validate($_POST);

        if ($errors === []) {
            $result = $service->create([
                ...$_POST,

                'source_page' =>
                    $_SERVER['HTTP_REFERER'] ??
                    '/experience/book-demo.php',

                'source_campaign' =>
                    $_GET['campaign'] ??
                    $_POST['source_campaign'] ??
                    null,
            ]);

            $_SESSION['hyro_demo_success'] = [
                'lead_code' => $result['lead_code'],
                'contact_name' => trim(
                    (string) $_POST['contact_name']
                ),
                'product_name' =>
                    $allowedProducts[$selectedProduct],
            ];

            hyro_redirect(
                '/experience/demo-thank-you.php'
            );
        }
    } catch (Throwable $exception) {
        hyro_log(
            'leads',
            'Book Demo submission failed.',
            [
                'error' => $exception->getMessage(),
            ]
        );

        $errors['general'] =
            'We could not submit your request. Please try again.';
    }
}

$pageTitle = 'Book a Live Demo | HYRO ONE';

?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0"
    >

    <meta
        name="robots"
        content="noindex, nofollow"
    >

    <title>
        <?= hyro_escape($pageTitle) ?>
    </title>

    <link
        rel="icon"
        href="/experience/assets/icons/favicon.svg"
        type="image/svg+xml"
    >

    <link
        rel="stylesheet"
        href="/experience/assets/css/site.css?v=<?= time() ?>"
    >

    <link
        rel="stylesheet"
        href="/experience/assets/css/book-demo.css?v=<?= time() ?>"
    >

</head>

<body>

<header class="hyro-nav">

    <div class="hyro-container hyro-nav-inner">

        <a href="/experience/" class="hyro-brand">

            <strong>HYRO ONE</strong>

            <span>
                ONE PLATFORM. UNLIMITED BUSINESS.
            </span>

        </a>

        <a
            href="/experience/"
            class="hyro-button hyro-button-secondary"
        >
            Back to Website
        </a>

    </div>

</header>

<main class="book-demo-page">

    <div class="hyro-container book-demo-layout">

        <section class="book-demo-intro">

            <div class="homepage-section-eyebrow">
                SCHEDULE A LIVE DEMO
            </div>

            <h1>
                See HYRO ONE
                <span>in action.</span>
            </h1>

            <p>
                Tell us about your business and our team will
                arrange a personalized product demonstration.
            </p>

            <div class="book-demo-benefits">

                <div class="book-demo-benefit">
                    <span>✓</span>
                    Personalized product walkthrough
                </div>

                <div class="book-demo-benefit">
                    <span>✓</span>
                    Questions answered by our team
                </div>

                <div class="book-demo-benefit">
                    <span>✓</span>
                    No installation required
                </div>

                <div class="book-demo-benefit">
                    <span>✓</span>
                    Explore pricing and onboarding
                </div>

            </div>

        </section>

        <section class="book-demo-card">

            <h2>Book your demo</h2>

            <p>
                Complete the form and our team will contact you.
            </p>

            <?php if (isset($errors['general'])): ?>

                <div class="book-demo-alert">
                    <?= hyro_escape($errors['general']) ?>
                </div>

            <?php endif; ?>

            <form
                method="post"
                action=""
                novalidate
            >

                <?= HyroCsrf::input() ?>

                <input
                    type="hidden"
                    name="source_campaign"
                    value="<?= hyro_old(
                        'source_campaign',
                        $old
                    ) ?>"
                >

                <div class="book-demo-honeypot">

                    <label for="website">
                        Website
                    </label>

                    <input
                        type="text"
                        id="website"
                        name="website"
                        tabindex="-1"
                        autocomplete="off"
                    >

                </div>

                <div class="book-demo-grid">

                    <div class="book-demo-field full">

                        <label for="product_code">
                            Product
                        </label>

                        <select
                            id="product_code"
                            name="product_code"
                            required
                        >

                            <?php foreach (
                                $allowedProducts as
                                $code => $name
                            ): ?>

                                <option
                                    value="<?= hyro_escape($code) ?>"
                                    <?= $selectedProduct === $code
                                        ? 'selected'
                                        : '' ?>
                                >
                                    <?= hyro_escape($name) ?>
                                </option>

                            <?php endforeach; ?>

                        </select>

                        <?php if (
                            isset($errors['product_code'])
                        ): ?>

                            <div class="book-demo-error">
                                <?= hyro_escape(
                                    $errors['product_code']
                                ) ?>
                            </div>

                        <?php endif; ?>

                    </div>

                    <div class="book-demo-field">

                        <label for="company_name">
                            Company Name
                        </label>

                        <input
                            type="text"
                            id="company_name"
                            name="company_name"
                            value="<?= hyro_old(
                                'company_name',
                                $old
                            ) ?>"
                            maxlength="150"
                            required
                        >

                        <?php if (
                            isset($errors['company_name'])
                        ): ?>

                            <div class="book-demo-error">
                                <?= hyro_escape(
                                    $errors['company_name']
                                ) ?>
                            </div>

                        <?php endif; ?>

                    </div>

                    <div class="book-demo-field">

                        <label for="contact_name">
                            Contact Person
                        </label>

                        <input
                            type="text"
                            id="contact_name"
                            name="contact_name"
                            value="<?= hyro_old(
                                'contact_name',
                                $old
                            ) ?>"
                            maxlength="150"
                            required
                        >

                        <?php if (
                            isset($errors['contact_name'])
                        ): ?>

                            <div class="book-demo-error">
                                <?= hyro_escape(
                                    $errors['contact_name']
                                ) ?>
                            </div>

                        <?php endif; ?>

                    </div>

                    <div class="book-demo-field">

                        <label for="email">
                            Email Address
                        </label>

                        <input
                            type="email"
                            id="email"
                            name="email"
                            value="<?= hyro_old(
                                'email',
                                $old
                            ) ?>"
                            maxlength="190"
                            required
                        >

                        <?php if (
                            isset($errors['email'])
                        ): ?>

                            <div class="book-demo-error">
                                <?= hyro_escape(
                                    $errors['email']
                                ) ?>
                            </div>

                        <?php endif; ?>

                    </div>

                    <div class="book-demo-field">

                        <label for="phone">
                            Phone / WhatsApp
                        </label>

                        <input
                            type="tel"
                            id="phone"
                            name="phone"
                            value="<?= hyro_old(
                                'phone',
                                $old
                            ) ?>"
                            maxlength="40"
                            required
                        >

                        <?php if (
                            isset($errors['phone'])
                        ): ?>

                            <div class="book-demo-error">
                                <?= hyro_escape(
                                    $errors['phone']
                                ) ?>
                            </div>

                        <?php endif; ?>

                    </div>

                    <div class="book-demo-field">

                        <label for="country_code">
                            Business Country
                        </label>

                        <select
                            id="country_code"
                            name="country_code"
                            required
                        >

                            <option value="">
                                Select Country
                            </option>

                            <?php foreach (
                                $countries as
                                $code => $country
                            ): ?>

                                <option
                                    value="<?= hyro_escape($code) ?>"
                                    <?= (
                                        ($old['country_code'] ?? 'AE')
                                        === $code
                                    )
                                        ? 'selected'
                                        : '' ?>
                                >
                                    <?= hyro_escape(
                                        $country['name']
                                    ) ?>
                                </option>

                            <?php endforeach; ?>

                        </select>

                        <?php if (
                            isset($errors['country_code'])
                        ): ?>

                            <div class="book-demo-error">
                                <?= hyro_escape(
                                    $errors['country_code']
                                ) ?>
                            </div>

                        <?php endif; ?>

                    </div>

                    <div class="book-demo-field">

                        <label for="city">
                            City
                        </label>

                        <input
                            type="text"
                            id="city"
                            name="city"
                            value="<?= hyro_old(
                                'city',
                                $old
                            ) ?>"
                            maxlength="100"
                        >

                    </div>

                    <div class="book-demo-field">

                        <label for="preferred_date">
                            Preferred Demo Date
                        </label>

                       <input
    type="date"
    id="preferred_date"
    name="preferred_date"
    value="<?= hyro_old(
        'preferred_date',
        $old
    ) ?>"
    min="<?= date('Y-m-d') ?>"
    required
>
                        <?php if (
                            isset($errors['preferred_date'])
                        ): ?>

                            <div class="book-demo-error">
                                <?= hyro_escape(
                                    $errors['preferred_date']
                                ) ?>
                            </div>

                        <?php endif; ?>

                    </div>

                    <div class="book-demo-field">

                        <label for="preferred_time">
                            Preferred Time
                        </label>

                       <select
    id="preferred_time"
    name="preferred_time"
    required
>
                            <option value="">
                                Select Time
                            </option>

                            <?php
                            $timeOptions = [
                                '09:00 AM - 11:00 AM',
                                '11:00 AM - 01:00 PM',
                                '02:00 PM - 04:00 PM',
                                '04:00 PM - 06:00 PM',
                            ];

                            foreach ($timeOptions as $time):
                            ?>

                                <option
                                    value="<?= hyro_escape($time) ?>"
                                    <?= (
                                        ($old['preferred_time'] ?? '')
                                        === $time
                                    )
                                        ? 'selected'
                                        : '' ?>
                                >
                                    <?= hyro_escape($time) ?>
                                </option>

                            <?php endforeach; ?>

                        </select>
                        <?php if (
    isset($errors['preferred_time'])
): ?>

    <div class="book-demo-error">
        <?= hyro_escape(
            $errors['preferred_time']
        ) ?>
    </div>

<?php endif; ?>

                    </div>

                    <div class="book-demo-field full">

                        <label for="message">
                            Tell us about your requirements
                        </label>

                        <textarea
                            id="message"
                            name="message"
                            maxlength="2000"
                            placeholder="Products, branches, users or any specific requirements..."
                        ><?= hyro_old(
                            'message',
                            $old
                        ) ?></textarea>

                    </div>

                </div>

                <div class="book-demo-actions">

                    <button
                        type="submit"
                        class="hyro-button hyro-button-primary"
                    >
                        Schedule My Live Demo →
                    </button>

                </div>

                <div class="book-demo-privacy">
                    By submitting this form, you agree that
                    HYRO TECHNOLOGIES FZE may contact you
                    regarding your demonstration request.
                </div>

            </form>

        </section>

    </div>

</main>

</body>
</html>
