<?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['token'])) {
    echo json_encode(['success' => false, 'message' => 'Token missing']);
    exit;
  }

  $token = $_GET['token'];

  $stmt = $pdo->prepare("SELECT i.invitation_id, iv.invitee_id, iv.name, iv.response_status FROM invitations i JOIN invitees iv ON i.invitation_id = iv.invitation_id WHERE i.token = ?");
  $stmt->execute([$token]);
  $invitees = $stmt->fetchAll(PDO::FETCH_ASSOC);

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

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