<?php
/**
 * API: Send Contact Form via SMTP
 * POST /api/send-contact.php
 * Replaces the old external API call in contact.php
 */
session_start();
header('Content-Type: application/json');

require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../smtp_mailer.php';

// Basic validation
$full_name    = trim($_POST['full_name'] ?? '');
$organization = trim($_POST['organization'] ?? '');
$email        = trim($_POST['email'] ?? '');
$phone        = trim($_POST['phone'] ?? '');
$inquiry_type = trim($_POST['inquiry_type'] ?? '');
$message      = trim($_POST['message'] ?? '');

if ($full_name === '' || $email === '' || $inquiry_type === '' || $message === '') {
    echo json_encode(['ok' => false, 'error' => 'Please fill in all required fields.']);
    exit;
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo json_encode(['ok' => false, 'error' => 'Invalid email address.']);
    exit;
}

// Sanitize
$full_name    = htmlspecialchars($full_name, ENT_QUOTES, 'UTF-8');
$organization = htmlspecialchars($organization, ENT_QUOTES, 'UTF-8');
$email        = htmlspecialchars($email, ENT_QUOTES, 'UTF-8');
$phone        = htmlspecialchars($phone, ENT_QUOTES, 'UTF-8');
$inquiry_type = htmlspecialchars($inquiry_type, ENT_QUOTES, 'UTF-8');
$message_safe = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');

// Build email body
$body = "
<h2 style='margin-bottom:10px;'>New Contact Form Inquiry</h2>
<table style='border-collapse:collapse;width:100%;max-width:600px;'>
<tr><td style='padding:8px;font-weight:bold;background:#f8fafc;border:1px solid #e2e8f0;'>Name</td>
    <td style='padding:8px;border:1px solid #e2e8f0;'>{$full_name}</td></tr>
<tr><td style='padding:8px;font-weight:bold;background:#f8fafc;border:1px solid #e2e8f0;'>Organization</td>
    <td style='padding:8px;border:1px solid #e2e8f0;'>{$organization}</td></tr>
<tr><td style='padding:8px;font-weight:bold;background:#f8fafc;border:1px solid #e2e8f0;'>Email</td>
    <td style='padding:8px;border:1px solid #e2e8f0;'><a href='mailto:{$email}'>{$email}</a></td></tr>
<tr><td style='padding:8px;font-weight:bold;background:#f8fafc;border:1px solid #e2e8f0;'>Phone</td>
    <td style='padding:8px;border:1px solid #e2e8f0;'>{$phone}</td></tr>
<tr><td style='padding:8px;font-weight:bold;background:#f8fafc;border:1px solid #e2e8f0;'>Topic</td>
    <td style='padding:8px;border:1px solid #e2e8f0;'>{$inquiry_type}</td></tr>
<tr><td style='padding:8px;font-weight:bold;background:#f8fafc;border:1px solid #e2e8f0;' colspan='2'>Message</td></tr>
<tr><td colspan='2' style='padding:12px;border:1px solid #e2e8f0;white-space:pre-wrap;'>{$message_safe}</td></tr>
</table>
<p style='margin-top:16px;font-size:12px;color:#94a3b8;'>Sent from the SaniBook Contact Form</p>
";

$subject = "SaniBook Contact: {$inquiry_type} - {$full_name}";

$mailer = new SmtpMailer();
$sent = $mailer->send('info@sanibook.ke', $subject, $body);

if ($sent) {
    echo json_encode(['ok' => true]);
} else {
    error_log("Contact form SMTP error: " . $mailer->getLastError());
    echo json_encode(['ok' => false, 'error' => 'Failed to send message. Please try again later.']);
}
