<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $message = trim($_POST['message']);

    if (empty($name) || empty($email) || empty($message) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $_SESSION['error'] = "Please fill out all fields with valid information.";
        header("Location: /#contact");
        exit();
    }

    require_once 'mail/MailService.php';
    $mailResult = MailService::sendContactMessage($name, $email, $message, 'vo.info.contact@gmail.com');

    if (!empty($mailResult['success'])) {
        $_SESSION['success'] = "Thank you for your message. We will get back to you shortly.";
    } else {
        $_SESSION['error'] = "Sorry, there was an error sending your message. Please try again later.";
        if (!empty($mailResult['error'])) {
            error_log("MailService Error: " . $mailResult['error']);
        }
    }

    header("Location: /#contact");
    exit();
} else {
    header("Location: /");
    exit();
}
