<?php
// recover_registration.php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// 1. Include your existing DB connection
require 'db_connection.php'; 
// 2. Include PHPMailer (Adjust path as necessary)
require 'vendor/autoload.php'; 

// --- CONFIGURATION ---
$target_id = 45; // <--- REPLACE THIS with the ID of the user who didn't get the email
// ---------------------

// 3. Fetch the user details
$stmt = $conn->prepare("SELECT * FROM registrations WHERE id = ?");
$stmt->bind_param("i", $target_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $user = $result->fetch_assoc();
    $email = $user['email'];     // <--- Check your DB column name
    $name  = $user['name'];      // <--- Check your DB column name
    
    // 4. Update the Database Status to 'Success' or 'Paid'
    $updateStmt = $conn->prepare("UPDATE registrations SET payment_status = 'Success' WHERE id = ?");
    $updateStmt->bind_param("i", $target_id);
    
    if ($updateStmt->execute()) {
        echo "Database updated successfully for ID: $target_id.<br>";
        
        // 5. Send the Email manually
        if (sendConfirmationEmail($email, $name)) {
            echo "<strong>Success!</strong> Email has been sent to $email.";
        } else {
            echo "<strong>Error:</strong> Database updated, but Email failed to send.";
        }
    } else {
        echo "Error updating database: " . $conn->error;
    }
} else {
    echo "No record found with ID: $target_id";
}

// --- FUNCTION TO SEND EMAIL ---
function sendConfirmationEmail($toEmail, $userName) {
    $mail = new PHPMailer(true);
    try {
        // Server settings
        $mail->isSMTP();
        $mail->Host       = 'smtp.gmail.com'; // Or your host
        $mail->SMTPAuth   = true;
        $mail->Username   = 'your_email@gmail.com'; // YOUR EMAIL
        $mail->Password   = 'your_app_password';    // YOUR PASSWORD
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port       = 587;

        // Recipients
        $mail->setFrom('no-reply@unitedculturalforum.com', 'UCF Team');
        $mail->addAddress($toEmail, $userName);

        // Content
        $mail->isHTML(true);
        $mail->Subject = 'Registration Confirmed - UCF';
        $mail->Body    = "Hello $userName, <br><br>Your payment was successful and your registration is confirmed.<br><br>Regards,<br>UCF Team";

        $mail->send();
        return true;
    } catch (Exception $e) {
        echo "Mailer Error: {$mail->ErrorInfo}";
        return false;
    }
}
?>