<?php
require_once __DIR__ . '/config/db.php';

try {
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // 1. Ensure Classes Exist
    $classes = ['XII IPA 1', 'XII IPA 2', 'XII IPA 3'];
    $class_ids = [];
    
    foreach ($classes as $name) {
        $stmt = $pdo->prepare("SELECT id FROM kelas WHERE nama = ?");
        $stmt->execute([$name]);
        $id = $stmt->fetchColumn();
        
        if (!$id) {
            $stmt = $pdo->prepare("INSERT INTO kelas (nama) VALUES (?)");
            $stmt->execute([$name]);
            $id = $pdo->lastInsertId();
        }
        $class_ids[$name] = $id;
    }

    // 2. Clear existing students if any (optional, but requested for 'tambahkan' maybe we just append)
    // To avoid duplicates on NIS, let's keep it additive.

    $prefixes_male = ['Ahmad', 'Budi', 'Cahyo', 'Dedi', 'Eko', 'Fajar', 'Guntur', 'Hadi', 'Iwan', 'Joko', 'Kurniawan', 'Lukman', 'Maulana', 'Nugroho', 'Oki', 'Prasetyo', 'Rian', 'Suryo', 'Taufik', 'Umar', 'Vicky', 'Wahyu', 'Xander', 'Yanto', 'Zaki'];
    $prefixes_female = ['Anisa', 'Bunga', 'Citra', 'Dwi', 'Eka', 'Fitri', 'Gita', 'Hana', 'Indah', 'Julia', 'Kartika', 'Lestari', 'Maya', 'Nanda', 'Olive', 'Putri', 'Qonita', 'Rina', 'Sari', 'Tia', 'Utami', 'Vina', 'Wulan', 'Xena', 'Yanti', 'Zahra'];
    
    $suffixes = ['Saputra', 'Pratama', 'Hidayat', 'Ramadhan', 'Wijaya', 'Kusuma', 'Santoso', 'Gunawan', 'Setiawan', 'Budiman'];
    $suffixes_f = ['Lestari', 'Putri', 'Permata', 'Sari', 'Wulandari', 'Anggraini', 'Utami', 'Azahra', 'Kurnia', 'Rahmawati'];

    $npsn_part = "10214"; // 5 digits
    $year_part = "24";    // 2 digits
    
    $student_count = 1;
    
    foreach ($class_ids as $c_name => $c_id) {
        echo "Seeding students for $c_name...\n";
        for ($i = 1; $i <= 30; $i++) {
            $is_male = rand(0, 1) === 0;
            $first = $is_male ? $prefixes_male[array_rand($prefixes_male)] : $prefixes_female[array_rand($prefixes_female)];
            $last = $is_male ? $suffixes[array_rand($suffixes)] : $suffixes_f[array_rand($suffixes_f)];
            $name = $first . " " . $last;
            
            $gender = $is_male ? 'L' : 'P';
            $nis = $year_part . $npsn_part . str_pad($student_count, 3, '0', STR_PAD_LEFT);
            $alamat = "Jl. Lintas Sumatera No. " . rand(1, 100) . ", Padang Lawas";
            
            try {
                $stmt = $pdo->prepare("INSERT INTO siswa (nis, nama, alamat, kelamin, id_kelas, password) VALUES (?, ?, ?, ?, ?, ?)");
                // Password default: 123456
                $stmt->execute([$nis, $name, $alamat, $gender, $c_id, password_hash('123456', PASSWORD_DEFAULT)]);
                $student_count++;
            } catch (Exception $e) {
               // Duplicate NIS skip
            }
        }
    }

    echo "Successfully seeded " . ($student_count - 1) . " students across 3 classes.\n";
    echo "Default Password for all students: 123456\n";

} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
