<?php
require_once __DIR__ . '/../../cors.php'; // Enable CORS for React Native
// POST /api/contact — submit contact message (public, no auth)
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../middleware/auth.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') jsonError('Method not allowed', 405);

$body    = getBody();
$name    = trim($body['name']    ?? '');
$phone   = trim($body['phone']   ?? '');
$service = trim($body['service'] ?? '');
$message = trim($body['message'] ?? '');

if (!$name || !$phone || !$message) jsonError('name, phone and message are required');

$db = getDB();
$id = generateId();
$db->prepare(
    'INSERT INTO messages (id, name, email, phone, subject, message, created_at) VALUES (?, ?, ?, ?, ?, ?, NOW())'
)->execute([$id, $name, $body['email'] ?? null, $phone, $service ?: null, $message]);

jsonResponse(['message' => 'Message sent', 'id' => $id], 201);
