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

header('Content-Type: text/html; charset=utf-8');

echo "<h2>Adding sample students to the database...</h2>\n";

// First, let's check if we have any classes to assign students to
$stmt = $pdo->query("SELECT id, name FROM classes WHERE status = 'active' LIMIT 5");
$classes = $stmt->fetchAll();

if (empty($classes)) {
    echo "<p>No active classes found. Creating sample classes first...</p>\n";
    
    // Create some sample classes
    $sample_classes = [
        ['name' => 'প্রথম শ্রেণি', 'section' => 'ক', 'capacity' => 40],
        ['name' => 'দ্বিতীয় শ্রেণি', 'section' => 'ক', 'capacity' => 40],
        ['name' => 'তৃতীয় শ্রেণি', 'section' => 'ক', 'capacity' => 40],
        ['name' => 'চতুর্থ শ্রেণি', 'section' => 'ক', 'capacity' => 40],
        ['name' => 'পঞ্চম শ্রেণি', 'section' => 'ক', 'capacity' => 40]
    ];
    
    foreach ($sample_classes as $class) {
        $stmt = $pdo->prepare("INSERT INTO classes (name, section, capacity, status) VALUES (?, ?, ?, 'active')");
        $stmt->execute([$class['name'], $class['section'], $class['capacity']]);
        echo "<p>Created class: {$class['name']} {$class['section']}</p>\n";
    }
    
    // Get the classes again
    $stmt = $pdo->query("SELECT id, name FROM classes WHERE status = 'active' LIMIT 5");
    $classes = $stmt->fetchAll();
}

// Sample students data
$sample_students = [
    ['first_name' => 'রহিম', 'last_name' => 'আহমেদ', 'roll_number' => '1', 'session' => '2023-2024', 'group_name' => 'General'],
    ['first_name' => 'করিম', 'last_name' => 'হোসেন', 'roll_number' => '2', 'session' => '2023-2024', 'group_name' => 'General'],
    ['first_name' => 'রহমান', 'last_name' => 'শেখ', 'roll_number' => '3', 'session' => '2023-2024', 'group_name' => 'General'],
    ['first_name' => 'তারেক', 'last_name' => 'মাহমুদ', 'roll_number' => '4', 'session' => '2023-2024', 'group_name' => 'Science'],
    ['first_name' => 'সালমা', 'last_name' => 'বেগম', 'roll_number' => '5', 'session' => '2023-2024', 'group_name' => 'Science'],
    ['first_name' => 'আরিফ', 'last_name' => 'ইসলাম', 'roll_number' => '6', 'session' => '2023-2024', 'group_name' => 'Commerce'],
    ['first_name' => 'নূর', 'last_name' => 'মহিলা', 'roll_number' => '7', 'session' => '2023-2024', 'group_name' => 'Arts'],
    ['first_name' => 'মাসুম', 'last_name' => 'বিল্লাহ', 'roll_number' => '8', 'session' => '2024-2025', 'group_name' => 'General'],
    ['first_name' => 'ফারহানা', 'last_name' => 'আক্তার', 'roll_number' => '9', 'session' => '2024-2025', 'group_name' => 'Science'],
    ['first_name' => 'রিফাত', 'last_name' => 'জাহান', 'roll_number' => '10', 'session' => '2024-2025', 'group_name' => 'Commerce']
];

// Get the first academic session if available, otherwise create one
$stmt = $pdo->query("SELECT id, session_name FROM academic_sessions WHERE status = 'active' LIMIT 1");
$session = $stmt->fetch();

if (!$session) {
    echo "<p>No active academic session found. Creating a sample session...</p>\n";
    $stmt = $pdo->prepare("INSERT INTO academic_sessions (session_name, start_date, end_date, status) VALUES (?, ?, ?, 'active')");
    $stmt->execute(['2023-2024', '2023-01-01', '2024-12-31', 'active']);
    
    $stmt = $pdo->query("SELECT id, session_name FROM academic_sessions WHERE status = 'active' LIMIT 1");
    $session = $stmt->fetch();
}

$session_id = $session['id'];

$added_count = 0;

foreach ($sample_students as $index => $student) {
    // Select a class randomly from available classes
    $class = $classes[$index % count($classes)];
    
    // Generate a student ID
    $new_id = 1000 + $index + 1;
    $student_id = 'ST' . str_pad($new_id, 4, '0', STR_PAD_LEFT);
    
    try {
        $stmt = $pdo->prepare("
            INSERT INTO students 
            (student_id, first_name, last_name, roll_number, session, session_id, group_name, class_id, 
            email, phone, address, date_of_birth, gender, status, created_at, updated_at)
            VALUES 
            (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
        ");
        
        $stmt->execute([
            $student_id,
            $student['first_name'],
            $student['last_name'],
            $student['roll_number'],
            $student['session'],
            $session_id,
            $student['group_name'],
            $class['id'],
            "student{$new_id}@example.com",
            "01" . str_pad($new_id, 8, '0', STR_PAD_LEFT),
            'ঠিকানা নেই',
            '2010-01-01',
            'Male',
            'active'
        ]);
        
        $added_count++;
        echo "<p>Added student: {$student['first_name']} {$student['last_name']} (Class: {$class['name']}, Roll: {$student['roll_number']})</p>\n";
    } catch (Exception $e) {
        echo "<p>Error adding student {$student['first_name']}: " . $e->getMessage() . "</p>\n";
    }
}

echo "<h3>Successfully added $added_count sample students to the database.</h3>\n";
echo "<p>You should now be able to see results on the search page.</p>\n";
echo "<p><a href='students/search.php'>Click here to go to the search page</a></p>\n";

// Clean up by deleting this file after execution
if (file_exists(__FILE__)) {
    unlink(__FILE__);
    echo "<p>Temporary script file deleted.</p>\n";
}
?>