<?php
header('Content-Type: application/json');
require_once '/home12/fiberule/public_html/kartikthanaweddingdinner/config/db.php';

// 1. Capture the data including the new 'source' field
$name    = trim($_POST['name'] ?? '');
$message = trim($_POST['message'] ?? '');
$source  = trim($_POST['source'] ?? 'Invitation'); // Defaults to Invitation if not specified

// 2. Validation
if (!$name || !$message) {
    echo json_encode(["success" => false, "error" => "Missing name or message"]);
    exit;
}

try {
    // 3. Updated Query to include the 'source' column
    $stmt = $pdo->prepare("INSERT INTO wishes (name, message, source) VALUES (?, ?, ?)");
    $ok = $stmt->execute([$name, $message, $source]);

    echo json_encode(["success" => $ok]);
} catch (PDOException $e) {
    // This helps you see the error if the database rejects the save
    echo json_encode(["success" => false, "error" => $e->getMessage()]);
}