<?php
require_once 'config/database.php';
require_once 'includes/uddoktapay.php';

echo "Testing payment processing...\n";

// Test data
$student_id = 6189; // Using a test student ID
$pay_amount = 100.00;
$payment_method = 'online';
$transaction_id = 'TEST-12345';
$user_id = 1; // Admin user ID for testing

try {
    echo "Processing payment for student ID: $student_id\n";
    echo "Amount: $pay_amount\n";
    echo "Transaction ID: $transaction_id\n";
    
    // Get student details
    echo "Fetching student details...\n";
    $stmt = $pdo->prepare("SELECT s.*, c.name as class_name, c.section FROM students s LEFT JOIN classes c ON s.class_id=c.id WHERE s.id=? LIMIT 1");
    $stmt->execute([$student_id]);
    $student = $stmt->fetch();
    
    if (!$student) {
        echo "Error: Student not found.\n";
        exit(1);
    }
    
    echo "Student found: " . $student['first_name'] . " " . $student['last_name'] . "\n";
    
    // Start database transaction
    echo "Starting database transaction...\n";
    $pdo->beginTransaction();
    
    // Check if fee already exists for this student and fee type
    echo "Checking for existing fee record...\n";
    $check_stmt = $pdo->prepare(
        "SELECT id FROM fees WHERE student_id = ? AND fee_type = 'অনলাইন পেমেন্ট' AND status != 'cancelled' LIMIT 1"
    );
    $check_stmt->execute([$student_id]);
    $existing_fee = $check_stmt->fetch();
    
    if (!$existing_fee) {
        // Insert a new fee record for this payment
        echo "Inserting fee record...\n";
        $stmt = $pdo->prepare("INSERT INTO fees (student_id, fee_type, amount, paid_amount, due_date, created_at) VALUES (?, 'অনলাইন পেমেন্ট', ?, ?, CURDATE(), NOW())");
        $stmt->execute([$student_id, $pay_amount, $pay_amount]);
    } else {
        echo "Fee record already exists for this student and fee type.\n";
        // Optionally update the existing fee if needed
        $stmt = $pdo->prepare("UPDATE fees SET paid_amount = paid_amount + ?, amount = amount + ?, updated_at = NOW() WHERE id = ?");
        $stmt->execute([$pay_amount, $pay_amount, $existing_fee['id']]);
    }
    if (!$existing_fee) {
        $fee_id = $pdo->lastInsertId();
        echo "Fee record inserted with ID: $fee_id\n";
    } else {
        $fee_id = $existing_fee['id'];
        echo "Existing fee record updated with ID: $fee_id\n";
    }
    
    // Insert payment record
    echo "Inserting payment record...\n";
    $stmt = $pdo->prepare("INSERT INTO fee_payments (fee_id, amount, payment_date, payment_method, transaction_id, notes, created_by) VALUES (?, ?, CURDATE(), ?, ?, ?, ?)");
    $stmt->execute([
        $fee_id, 
        $pay_amount, 
        $payment_method, 
        $transaction_id, 
        'Online payment via UddoktaPay',
        $user_id
    ]);
    
    $payment_id = $pdo->lastInsertId();
    echo "Payment record inserted with ID: $payment_id\n";
    
    $pdo->commit();
    echo "Transaction committed successfully!\n";
    
} catch (Exception $e) {
    $pdo->rollBack();
    echo "Error during payment processing: " . $e->getMessage() . "\n";
    echo "Stack trace: " . $e->getTraceAsString() . "\n";
}
?>