<?php
if (session_status() === PHP_SESSION_NONE) session_start();
require_once 'config/database.php';

// Récupérer les réglages SMTP
$stmt = $pdo->query("SELECT `key`, `value` FROM site_settings WHERE `key` LIKE 'smtp_%' OR `key` = 'contact_recipient'");
$settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['send_contact'])) {
    $captcha = (int)$_POST['captcha'];
    if ($captcha !== $_SESSION['captcha_sum']) {
        $message = "<div class='bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-6'>Erreur de calcul : le résultat du captcha est incorrect.</div>";
    } else {
        $name = htmlspecialchars($_POST['name']);
        $email = htmlspecialchars($_POST['email']);
        $subject = htmlspecialchars($_POST['subject']);
        $msg_content = htmlspecialchars($_POST['message']);
        
        $to = $settings['contact_recipient'] ?? 'mairie@kutzenhausen.fr';
        $headers = "From: $name <$email>\r\n";
        $headers .= "Reply-To: $email\r\n";
        $headers .= "Content-Type: text/plain; charset=UTF-8\r\n";
        
        $body = "Nouveau message depuis le site Kutzenhausen :\n\n";
        $body .= "Nom : $name\n";
        $body .= "Email : $email\n";
        $body .= "Sujet : $subject\n\n";
        $body .= "Message :\n$msg_content\n";
        
        require_once 'includes/mailer.php';
        if (send_site_notification($pdo, "Contact Site: $subject", $body)) {
            $message = "<div class='bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-6'>Votre message a bien été envoyé. Nous vous répondrons dans les plus brefs délais.</div>";
        } else {
            $message = "<div class='bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-6'>Une erreur est survenue lors de l'envoi de l'e-mail. Veuillez vérifier vos réglages SMTP en admin.</div>";
        }
    }
}

include 'includes/header.php';
?>

<main class="min-h-screen py-16 bg-gray-50">
    <div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">
        <h2 class="font-serif text-3xl font-bold text-alsace-red mb-8 border-b-2 border-alsace-red pb-4">
            Contactez-nous
        </h2>

        <?php if ($message): ?>
            <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-6">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>

        <div class="bg-white rounded-xl shadow-md p-8 border border-gray-100">
            <form action="" method="POST" class="space-y-6">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-sm font-bold text-gray-700 mb-2">Nom *</label>
                        <input type="text" name="name" required class="w-full border border-gray-300 rounded-md p-3 outline-none focus:border-alsace-red">
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-gray-700 mb-2">Email *</label>
                        <input type="email" name="email" required class="w-full border border-gray-300 rounded-md p-3 outline-none focus:border-alsace-red">
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-bold text-gray-700 mb-2">Sujet *</label>
                    <input type="text" name="subject" required class="w-full border border-gray-300 rounded-md p-3 outline-none focus:border-alsace-red">
                </div>

                <div>
                    <label class="block text-sm font-bold text-gray-700 mb-2">Message *</label>
                    <textarea name="message" rows="5" required class="w-full border border-gray-300 rounded-md p-3 outline-none focus:border-alsace-red"></textarea>
                </div>

                <?php
                $num1 = rand(1, 10);
                $num2 = rand(1, 10);
                $sum = $num1 + $num2;
                // On stocke la somme en session
                $_SESSION['captcha_sum'] = $sum;
                ?>

                <div class="bg-gray-50 p-4 rounded-lg border border-gray-200">
                    <label class="block text-sm font-bold text-gray-700 mb-2 text-center">Sécurité : Combien font <?php echo $num1; ?> + <?php echo $num2; ?> ? *</label>
                    <input type="number" name="captcha" required class="w-full border border-gray-300 rounded-md p-3 outline-none focus:border-alsace-red text-center font-bold">
                </div>

                <button type="submit" name="send_contact" class="w-full bg-alsace-red text-white font-bold py-3 px-4 rounded-md hover:bg-alsace-red-dark transition shadow-md">
                    Envoyer le message
                </button>
            </form>
        </div>
    </div>
</main>

<?php include 'includes/footer.php'; ?>
