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

echo "Test Student Insert\n";
echo "===================\n";

try {
    // Test data
    $test_data = [
        'user_id' => null,
        'student_id' => 'TEST2024001',
        'first_name' => 'Test',
        'last_name' => 'Student',
        'email' => null,
        'phone' => '01700000000',
        'address' => 'Test Address',
        'date_of_birth' => '2010-01-01',
        'gender' => 'male',
        'class_id' => 8, // Assuming class ID 8 exists
        'session' => '2024',
        'group_name' => 'A',
        'roll_number' => 99,
        'admission_date' => '2024-01-01',
        'guardian_name' => 'Test Guardian',
        'guardian_phone' => '01700000001',
        'guardian_email' => null,
        'guardian_address' => 'Guardian Address',
        'photo' => null
    ];
    
    echo "Test data:\n";
    foreach ($test_data as $key => $value) {
        echo "- $key: " . ($value ?? 'NULL') . "\n";
    }
    
    echo "\nInserting test student...\n";
    
    $stmt = $pdo->prepare("
        INSERT INTO students (
            user_id, student_id, first_name, last_name, email, phone, address,
            date_of_birth, gender, class_id, session, group_name, roll_number, admission_date,
            guardian_name, guardian_phone, guardian_email, guardian_address, photo
        ) VALUES (
            :user_id, :student_id, :first_name, :last_name, :email, :phone, :address,
            :date_of_birth, :gender, :class_id, :session, :group_name, :roll_number, :admission_date,
            :guardian_name, :guardian_phone, :guardian_email, :guardian_address, :photo
        )
    ");
    
    $stmt->execute([
        ':user_id' => $test_data['user_id'],
        ':student_id' => $test_data['student_id'],
        ':first_name' => $test_data['first_name'],
        ':last_name' => $test_data['last_name'],
        ':email' => $test_data['email'],
        ':phone' => $test_data['phone'],
        ':address' => $test_data['address'],
        ':date_of_birth' => $test_data['date_of_birth'],
        ':gender' => $test_data['gender'],
        ':class_id' => $test_data['class_id'],
        ':session' => $test_data['session'],
        ':group_name' => $test_data['group_name'],
        ':roll_number' => $test_data['roll_number'],
        ':admission_date' => $test_data['admission_date'],
        ':guardian_name' => $test_data['guardian_name'],
        ':guardian_phone' => $test_data['guardian_phone'],
        ':guardian_email' => $test_data['guardian_email'],
        ':guardian_address' => $test_data['guardian_address'],
        ':photo' => $test_data['photo']
    ]);
    
    $inserted_id = $pdo->lastInsertId();
    echo "✅ Student inserted successfully with ID: $inserted_id\n";
    
    // Verify the inserted data
    echo "\nVerifying inserted data...\n";
    $stmt = $pdo->prepare("SELECT id, student_id, first_name, last_name, roll_number, session, class_id, group_name FROM students WHERE id = ?");
    $stmt->execute([$inserted_id]);
    $result = $stmt->fetch();
    
    if ($result) {
        echo "Retrieved data:\n";
        echo "- ID: " . $result['id'] . "\n";
        echo "- Student ID: " . $result['student_id'] . "\n";
        echo "- Name: " . $result['first_name'] . " " . $result['last_name'] . "\n";
        echo "- Roll Number: " . $result['roll_number'] . "\n";
        echo "- Session: " . $result['session'] . "\n";
        echo "- Class ID: " . $result['class_id'] . "\n";
        echo "- Group: " . ($result['group_name'] ?? 'NULL') . "\n";
        
        // Check if data matches
        if ($result['roll_number'] == $test_data['roll_number'] && $result['session'] == $test_data['session']) {
            echo "✅ Data matches correctly!\n";
        } else {
            echo "❌ Data mismatch detected!\n";
            echo "Expected Roll: " . $test_data['roll_number'] . ", Got: " . $result['roll_number'] . "\n";
            echo "Expected Session: " . $test_data['session'] . ", Got: " . $result['session'] . "\n";
        }
    }
    
    // Clean up test data
    echo "\nCleaning up test data...\n";
    $pdo->exec("DELETE FROM students WHERE id = $inserted_id");
    echo "✅ Test data cleaned up\n";
    
} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "\n";
}
?>