<?php
// Williams Pressure Washing Services - Contact Form Email Handler
// Uses PHPMailer to connect directly to hMailserver on 10.10.9.31:25 (no auth, no TLS)
// hMailserver handles relay through Mailjet automatically

header('Content-Type: application/json');

// Only accept POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['error' => 'Method not allowed']);
    exit;
}

// Get JSON body
$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;

$name    = trim($input['name'] ?? '');
$phone   = trim($input['phone'] ?? '');
$email   = trim($input['email'] ?? '');
$service = trim($input['service'] ?? '');
$message = trim($input['message'] ?? '');

// --- Spam honeypot: if the hidden field has a value, it's a bot ---
if (!empty($input['website_url'] ?? '')) {
    // Bots fill this; humans don't. Silently succeed.
    echo json_encode(['success' => true]);
    exit;
}

// Validate required fields
$errors = [];
if (!$name) $errors[] = 'Name is required';
if (!$phone) $errors[] = 'Phone is required';
if (!$service) $errors[] = 'Service selection is required';

if (!empty($errors)) {
    http_response_code(400);
    echo json_encode(['error' => implode(', ', $errors)]);
    exit;
}

// Map service values to readable names
$serviceNames = [
    'truck-fleet' => 'Truck Fleet Washing',
    'bus-fleet'   => 'Bus Fleet Washing',
    'house'       => 'House Washing',
    'storefront'  => 'Storefront Cleaning',
    'other'       => 'Other',
];
$serviceDisplay = $serviceNames[$service] ?? $service;

// --- Email Configuration ---
$to      = 'waltwilliams87@gmail.com';
$subject = 'Quote Request: ' . $serviceDisplay . ' — ' . $name;

// Load PHPMailer
require_once __DIR__ . '/vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);

try {
    // Server settings — direct SMTP to hMailserver (no auth, no TLS)
    $mail->isSMTP();
    $mail->Host       = '10.10.9.31';
    $mail->Port       = 25;
    $mail->SMTPAuth   = false;
    $mail->SMTPAutoTLS = false;  // hMailserver advertises STARTTLS but we don't have a cert

    // Sender and recipients
    $mail->setFrom('noreply@WilliamsPressureWashingServices.com', 'Williams Pressure Washing');
    $mail->addAddress($to, 'Williams Pressure Washing');

    // Reply-to: the customer's email address
    if ($email) {
        $mail->addReplyTo($email, $name);
    }

    // Email content
    $mail->isHTML(false);
    $mail->CharSet = 'UTF-8';
    $mail->Subject = $subject;
    $mail->Body = "New Quote Request\n" .
        str_repeat('=', 40) . "\n\n" .
        "Name:      $name\n" .
        "Phone:     $phone\n" .
        "Email:     $email\n" .
        "Service:   $serviceDisplay\n\n" .
        "Message:\n$message\n\n" .
        str_repeat('=', 40) . "\n" .
        "Sent from WilliamsPressureWashingServices.com\n";

    $mail->send();
} catch (Exception $e) {
    // Log the error internally, don't expose to user
    error_log("WilliamsPW email failed: {$mail->ErrorInfo}");
}

// Always return success so the user sees their confirmation
echo json_encode(['success' => true]);
?>
