<?php
session_start();
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/database/schema.php';
require_once __DIR__ . '/models/functions.php';
require_once __DIR__ . '/functions.php';
initDatabase();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $subject = trim($_POST['subject'] ?? '');
    $message = trim($_POST['message'] ?? '');
    if ($name && $email && $message && verifyCaptcha($_POST['captcha'] ?? '')) {
        $db = getDB();
        $stmt = $db->prepare("INSERT INTO contact_messages (name, email, subject, message) VALUES (?, ?, ?, ?)");
        $stmt->execute([$name, $email, $subject, $message]);
        header('Location: /contact?sent=1');
        exit;
    } else {
        header('Location: /contact?error=1');
        exit;
    }
}
header('Location: /contact');
exit;
