<?php
header("Content-Type: application/json");

$host = 'localhost';
$db   = 'georizmj_wedding_TM';
$user = 'georizmj_wedadmin';
$pass = 'wedadminP@ss';

try {
  $pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user, $pass);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  if (!isset($_GET['invitee_id'], $_GET['status'], $_GET['token'])) {
    echo json_encode(['success' => false, 'message' => 'Missing parameters']);
    exit;
  }

  $invitee_id = intval($_GET['invitee_id']);
  $status = $_GET['status'];
  $token = $_GET['token'];

  $stmt = $pdo->prepare("
    UPDATE invitees iv
    JOIN invitations i ON iv.invitation_id = i.invitation_id
    SET iv.response_status = ?, iv.responded_at = NOW()
    WHERE iv.invitee_id = ? AND i.token = ?
  ");
  $stmt->execute([$status, $invitee_id, $token]);

  echo json_encode(['success' => true]);

} catch (PDOException $e) {
  echo json_encode(['success' => false, 'message' => 'Database error']);
}
?>