<?php
require_once __DIR__ . '/../db/config.php';

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

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

    if (empty($name) || empty($email)) {
        echo json_encode(['success' => false, 'error' => 'Name and email are required.']);
        exit;
    }

    try {
        $pdo = db();
        $stmt = $pdo->prepare("INSERT INTO inquiries (name, email, message, product_id) VALUES (?, ?, ?, ?)");
        $stmt->execute([$name, $email, $message, $product_id]);

        echo json_encode(['success' => true]);
    } catch (PDOException $e) {
        echo json_encode(['success' => false, 'error' => 'Database error.']);
    }
} else {
    echo json_encode(['success' => false, 'error' => 'Invalid request.']);
}
