<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

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


// 🔹 PHPMailer files include
require 'includes/phpmailer/src/PHPMailer.php';
require 'includes/phpmailer/src/SMTP.php';
require 'includes/phpmailer/src/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// 🔹 JSON data
$data = json_decode(file_get_contents("php://input"), true);

$email  = $data['email'] ?? '';
$name   = $data['name'] ?? '';
$amount = $data['amount'] ?? '';
$event  = $data['event'] ?? '';
$date   = $data['date'] ?? '';

// 🔹 validation
if (!$email) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Email missing'
    ]);
    exit;
}

$mail = new PHPMailer(true);

try {
    // 🔥 SMTP CONFIG (GMAIL)
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'edward.s@orbitdesignagency.com'; // tumhari gmail
    $mail->Password   = 'enze vilj ksgb foei'; // ⚠️ yahan app password
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;

    // 🔹 Sender
    $mail->setFrom('edward.s@orbitdesignagency.com', 'Sport Combine Sport Team');

    // 🔹 Receiver
    $mail->addAddress($email, $name);

    // 🔹 Email content
    $mail->isHTML(true);
    $mail->Subject = 'Here is your event invoice';

    $mail->Body = "
        <h2>Thank you, $name 🎉</h2>
        <p><b>Event:</b> $event</p>
        <p><b>Date:</b> $date</p>
        <p><b>Amount Paid:</b> $amount</p>
        <br>
        <p>Your registration is confirmed ✅</p>
    ";

    $mail->send();

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

} catch (Exception $e) {
    echo json_encode([
        'status' => 'error',
        'message' => $mail->ErrorInfo
    ]);
}