<?php
session_start();

$pageTitle = 'Contact PROLAD | Property Inspection & Construction Enquiries';
$activePage = 'contact';

$errors = [];
$success = false;
$name = '';
$phone = '';
$email = '';
$service = trim($_GET['service'] ?? '');
$message = '';
$property = trim($_GET['property'] ?? '');

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $service = trim($_POST['service'] ?? '');
    $message = trim($_POST['message'] ?? '');
    $property = trim($_POST['property'] ?? '');
    $mathAnswer = trim($_POST['math_answer'] ?? '');

    if ($name === '') $errors[] = 'Please enter your full name.';
    if ($phone === '') $errors[] = 'Please enter your phone number.';
    if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) $errors[] = 'Please enter a valid email address.';
    if ($service === '') $errors[] = 'Please select the service you need.';
    if ($message === '') $errors[] = 'Please tell us briefly how we can help.';
    if (
        $mathAnswer === '' ||
        !isset($_SESSION['contact_math_answer']) ||
        !hash_equals((string) $_SESSION['contact_math_answer'], $mathAnswer)
    ) {
        $errors[] = 'Please answer the security question correctly.';
    }

    // Expire each challenge after one submission attempt to prevent replay.
    unset($_SESSION['contact_math_answer'], $_SESSION['contact_math_question']);

    if (!$errors) {
        $recipient = 'support@prolad.com.ng';
        $safeName = preg_replace('/[\r\n]+/', ' ', $name);
        $subject = 'New PROLAD website enquiry from ' . $safeName;

        $emailBody = "A new enquiry was submitted through the PROLAD website.\n\n";
        $emailBody .= "Name: {$name}\n";
        $emailBody .= "Phone: {$phone}\n";
        $emailBody .= "Email: {$email}\n";
        $emailBody .= "Service: {$service}\n";
        if ($property !== '') {
            $emailBody .= "Property: {$property}\n";
        }
        $emailBody .= "\nMessage:\n{$message}\n";
        $emailBody .= "\nSubmitted: " . date('j F Y, g:i A') . "\n";

        $headers = [
            'From: PROLAD Website <noreply@prolad.com.ng>',
            'Reply-To: ' . $email,
            'Content-Type: text/plain; charset=UTF-8',
            'X-Mailer: PHP/' . phpversion(),
        ];

        if (@mail($recipient, $subject, wordwrap($emailBody, 70), implode("\r\n", $headers))) {
            $success = true;
            $name = $phone = $email = $service = $message = $property = '';
        } else {
            $errors[] = 'We could not send your enquiry right now. Please contact us by WhatsApp or phone.';
        }
    }
}

// Create a fresh, easy-to-read challenge for this form display.
if (!isset($_SESSION['contact_math_answer'], $_SESSION['contact_math_question'])) {
    $firstNumber = random_int(2, 9);
    $secondNumber = random_int(1, 9);
    $_SESSION['contact_math_question'] = $firstNumber . ' + ' . $secondNumber;
    $_SESSION['contact_math_answer'] = $firstNumber + $secondNumber;
}

$mathQuestion = $_SESSION['contact_math_question'];

require __DIR__ . '/includes/header.php';
?>

<main id="main-content">
    <section class="contact-hero">
        <div class="container contact-hero-grid">
            <div>
                <p class="eyebrow eyebrow-light">Contact PROLAD</p>
                <h1>Let’s build something<br><span>worth coming home to.</span></h1>
            </div>
            <p>Whether you need a property, a complete build, documentation support or architectural design, our team is ready to listen.</p>
        </div>
    </section>

    <section class="section contact-section">
        <div class="container contact-layout">
            <aside class="contact-details">
                <p class="eyebrow">Speak with our team</p>
                <h2>Start with a conversation.</h2>
                <p>Share what you have in mind and we’ll help you understand the clearest next step.</p>

                <div class="contact-methods">
                    <a href="tel:+2348145010155"><span>Primary phone</span><strong>0814 501 0155</strong></a>
                    <a href="tel:+2348032081085"><span>Alternative phone</span><strong>0803 208 1085</strong></a>
                    <a href="https://wa.me/2348145010155" target="_blank" rel="noopener"><span>WhatsApp</span><strong><i class="bi bi-whatsapp" aria-hidden="true"></i> +234 814 501 0155</strong></a>
                    <a href="mailto:support@prolad.com.ng"><span>Email</span><strong><i class="bi bi-envelope" aria-hidden="true"></i> support@prolad.com.ng</strong></a>
                </div>

                <div class="contact-hours">
                    <span>Business enquiries</span>
                    <p>Monday – Saturday<br>8:00 AM – 6:00 PM</p>
                </div>
            </aside>

            <div class="contact-form-panel">
                <?php if ($success): ?>
                    <div class="form-notice success" role="status">
                        <strong>Thank you for contacting PROLAD.</strong>
                        <p>Your enquiry has been received. Our team will contact you shortly.</p>
                    </div>
                <?php endif; ?>

                <?php if ($errors): ?>
                    <div class="form-notice error" role="alert">
                        <strong>Please check the following:</strong>
                        <ul><?php foreach ($errors as $error): ?><li><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8'); ?></li><?php endforeach; ?></ul>
                    </div>
                <?php endif; ?>

                <form class="contact-form" action="contact" method="post">
                    <div class="form-heading"><span>Project enquiry</span><h2>How can we help?</h2></div>

                    <?php if ($property !== ''): ?>
                        <div class="property-interest">Property enquiry: <strong><?= htmlspecialchars($property, ENT_QUOTES, 'UTF-8'); ?></strong></div>
                        <input type="hidden" name="property" value="<?= htmlspecialchars($property, ENT_QUOTES, 'UTF-8'); ?>">
                    <?php endif; ?>

                    <div class="form-row">
                        <label>Full name<input type="text" name="name" value="<?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8'); ?>" placeholder="Your name" autocomplete="name" required></label>
                        <label>Phone number<input type="tel" name="phone" value="<?= htmlspecialchars($phone, ENT_QUOTES, 'UTF-8'); ?>" placeholder="e.g. 0814 501 0155" autocomplete="tel" required></label>
                    </div>
                    <label>Email address<input type="email" name="email" value="<?= htmlspecialchars($email, ENT_QUOTES, 'UTF-8'); ?>" placeholder="you@example.com" autocomplete="email" required></label>
                    <label>I’m interested in
                        <select name="service" required>
                            <option value="" disabled <?= $service === '' ? 'selected' : ''; ?>>Select a service</option>
                            <?php foreach (['Real Estate', 'Construction', 'Property Documentation', 'Architectural Design'] as $option): ?>
                                <option <?= $service === $option ? 'selected' : ''; ?>><?= $option; ?></option>
                            <?php endforeach; ?>
                        </select>
                    </label>
                    <label>Tell us about your project<textarea name="message" rows="5" placeholder="Location, property type, project stage or other useful details" required><?= htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); ?></textarea></label>
                    <div class="math-security">
                        <div class="math-security-copy">
                            <span>Security check</span>
                            <strong>What is <?= htmlspecialchars($mathQuestion, ENT_QUOTES, 'UTF-8'); ?>?</strong>
                        </div>
                        <label>
                            <span class="visually-hidden">Answer to the maths security question</span>
                            <input type="number" name="math_answer" inputmode="numeric" placeholder="Answer" min="0" max="20" autocomplete="off" required>
                        </label>
                    </div>
                    <button class="button" type="submit">Send enquiry <span aria-hidden="true">→</span></button>
                    <p class="form-privacy">By submitting this form, you agree that PROLAD may contact you about your enquiry.</p>
                </form>
            </div>
        </div>
    </section>
</main>

<?php require __DIR__ . '/includes/footer.php'; ?>
