<?php
// File: subscription_payment.php

// ✅ Use the consistent session init file
require_once 'session_init.php';
require_once 'db.php';

// --- ROBUST SESSION CHECK ---
// Check 1: Is the registration data array even set?
// Check 2: Is the role 'artist'? (This is safer, as only artists should be here)
if (!isset($_SESSION['registration_data']) || $_SESSION['registration_data']['role'] !== 'artist') {
    // If not, the session is invalid or expired. Redirect back to register.
    header('Location: register.php?error=session_expired');
    exit();
}

// Check 3: Now that we know registration_data exists, check for the pack ID
// ✅ **FIX 1:** The key is 'subscription_pack_id', not 'pack_id'
if (!isset($_SESSION['registration_data']['subscription_pack_id'])) {
    // This case is unlikely but good to check
    header('Location: register.php?error=session_expired');
    exit();
}


// --- Session is Valid ---
$registration_data = $_SESSION['registration_data'];

// ✅ **FIX 2:** Get the correct variable
$pack_id = $registration_data['subscription_pack_id']; 

// Fetch the details of the selected pack from the database
$stmt = $conn->prepare("SELECT pack_name, price FROM subscription_packs WHERE id = ?");
$stmt->bind_param("i", $pack_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
    // If the pack ID from the session is invalid, something is wrong.
    $stmt->close(); // Close statement before redirecting
    header('Location: register.php?error=invalid_pack');
    exit();
}
$pack = $result->fetch_assoc();
$stmt->close();

include 'header.php'; 
?>

<div class="bg-gray-100 py-12 md:py-20">
    <div class="max-w-lg mx-auto px-4 sm:px-6 lg:px-8">
        <div class="bg-white p-8 rounded-xl shadow-lg">
            <h1 class="text-3xl font-extrabold text-ucf-charcoal text-center mb-6">Complete Your Registration</h1>
            
            <div class="border-t border-b border-gray-200 py-6">
                <h2 class="text-lg font-medium text-ucf-charcoal mb-4">Subscription Summary</h2>
                <dl class="space-y-4">
                        <div class="flex items-center justify-between">
                            <dt class="text-sm text-gray-600">Username</dt>
                            <dd class="text-sm font-medium text-ucf-charcoal"><?php echo htmlspecialchars($registration_data['username']); ?></dd>
                        </div>
                         <div class="flex items-center justify-between">
                            <dt class="text-sm text-gray-600">Email</dt>
                            <dd class="text-sm font-medium text-ucf-charcoal"><?php echo htmlspecialchars($registration_data['email']); ?></dd>
                        </div>
                        <div class="flex items-center justify-between">
                            <dt class="text-sm text-gray-600">Plan Selected</dt>
                            <dd class="text-sm font-medium text-ucf-charcoal"><?php echo htmlspecialchars($pack['pack_name']); ?></dd>
                        </div>
                        <div class="flex items-center justify-between">
                            <dt class="text-lg font-bold text-ucf-charcoal">Total Amount</dt>
                            <dd class="text-lg font-bold text-ucf-charcoal">₹<?php echo number_format($pack['price'], 2); ?></dd>
                        </div>
                </dl>
            </div>
            
            <div class="mt-6">
                <p class="text-center text-sm text-gray-500 mb-4">You will be redirected to our secure payment partner to complete the payment.</p>
                
                <!-- This form will now send the user to start the PayU process -->
                <form action="subscription_handler.php" method="POST">
                    <button type="submit" class="w-full flex items-center justify-center rounded-md border border-transparent bg-ucf-green px-6 py-3 text-base font-medium text-white shadow-sm hover:bg-ucf-green-dark">
                        Proceed to Pay
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>