<?php

require_once __DIR__ . '/db.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    header('Location: contact.html');
    exit;
}

$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$subject = trim($_POST['subject'] ?? '');
$category = trim($_POST['category'] ?? '');
$message = trim($_POST['message'] ?? '');

if ($name === '' || $email === '' || $subject === '' || $message === '') {
    header('Location: contact.html?error=1');
    exit;
}

try {
    $db = Database::getInstance();
    $db->saveContactRequest([
        'name'     => $name,
        'email'    => $email,
        'phone'    => $phone,
        'subject'  => $subject,
        'category' => $category,
        'message'  => $message,
    ]);

    header('Location: contact.html?success=1');
    exit;
} catch (Exception $e) {
    header('Location: contact.html?error=1');
    exit;
}
