<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';

// Define project root path
define('PADI_ROOT', dirname(__DIR__));

if (file_exists(dirname(__DIR__) . '/.env')) {
    Wibiesana\Padi\Core\Env::load(dirname(__DIR__) . '/.env');
}

try {
    $db = Wibiesana\Padi\Core\DatabaseManager::connection();
    
    // Clear existing data (disable FK checks temporarily)
    $db->exec("SET FOREIGN_KEY_CHECKS = 0");
    $db->exec("TRUNCATE TABLE sarpras_peminjaman");
    $db->exec("TRUNCATE TABLE sarpras_petugas");
    $db->exec("TRUNCATE TABLE sarpras_peminjam");
    $db->exec("TRUNCATE TABLE sarpras_items");
    $db->exec("SET FOREIGN_KEY_CHECKS = 1");

    echo "Truncated sarpras tables...\n";

    // 1. Seed sarpras_items
    $items = [
        [
            'kode_barang' => 'BRG-PROJ-01',
            'nama_barang' => 'Proyektor Epson EB-X400',
            'kategori' => 'Elektronik',
            'spesifikasi' => 'Resolusi XGA, 3300 Lumens, HDMI, VGA',
            'kondisi' => 'baik',
            'status' => 'tersedia',
            'lokasi_ruang' => 'Ruang Sarpras',
            'lokasi_rak' => 'Rak A1',
            'stok_total' => 5,
            'stok_tersedia' => 5,
            'gambar' => null,
            'qr_code_string' => 'ITEM-1',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ],
        [
            'kode_barang' => 'BRG-LAP-02',
            'nama_barang' => 'Laptop ASUS ExpertBook B1',
            'kategori' => 'Elektronik',
            'spesifikasi' => 'Intel Core i5, RAM 8GB, SSD 512GB, 14 Inch',
            'kondisi' => 'baik',
            'status' => 'tersedia',
            'lokasi_ruang' => 'Lab Komputer 1',
            'lokasi_rak' => 'Lemari B2',
            'stok_total' => 10,
            'stok_tersedia' => 10,
            'gambar' => null,
            'qr_code_string' => 'ITEM-2',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ],
        [
            'kode_barang' => 'BRG-KBL-03',
            'nama_barang' => 'Kabel Roll Terminal 10M',
            'kategori' => 'Kelistrikan',
            'spesifikasi' => 'Panjang 10 meter, 4 lubang stop kontak',
            'kondisi' => 'baik',
            'status' => 'tersedia',
            'lokasi_ruang' => 'Ruang Sarpras',
            'lokasi_rak' => 'Rak C3',
            'stok_total' => 8,
            'stok_tersedia' => 8,
            'gambar' => null,
            'qr_code_string' => 'ITEM-3',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ],
        [
            'kode_barang' => 'BRG-MIC-04',
            'nama_barang' => 'Wireless Microphone Shure',
            'kategori' => 'Audio',
            'spesifikasi' => 'Dual wireless hand-held mic, UHF band',
            'kondisi' => 'rusak',
            'status' => 'rusak',
            'lokasi_ruang' => 'Ruang Aula',
            'lokasi_rak' => 'Lemari Audio',
            'stok_total' => 2,
            'stok_tersedia' => 0,
            'gambar' => null,
            'qr_code_string' => 'ITEM-4',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ]
    ];

    $itemStmt = $db->prepare("INSERT INTO sarpras_items (kode_barang, nama_barang, kategori, spesifikasi, kondisi, status, lokasi_ruang, lokasi_rak, stok_total, stok_tersedia, gambar, qr_code_string, created_at, updated_at) VALUES (:kode_barang, :nama_barang, :kategori, :spesifikasi, :kondisi, :status, :lokasi_ruang, :lokasi_rak, :stok_total, :stok_tersedia, :gambar, :qr_code_string, :created_at, :updated_at)");
    foreach ($items as $item) {
        $itemStmt->execute($item);
    }
    echo "✓ Seeded sarpras_items successfully\n";

    // 2. Seed sarpras_peminjam
    $peminjams = [
        [
            'tipe_peminjam' => 'guru',
            'id_identitas' => '198910262022211006',
            'nama_lengkap' => 'Budi Santoso, S.Pd.',
            'kelas' => null,
            'jurusan' => null,
            'nomor_telepon' => '081234567890',
            'email' => 'budi.santoso@sekolah.sch.id',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ],
        [
            'tipe_peminjam' => 'siswa',
            'id_identitas' => '222310156',
            'nama_lengkap' => 'Andi Wijaya',
            'kelas' => 'X RPL 1',
            'jurusan' => 'Rekayasa Perangkat Lunak',
            'nomor_telepon' => '087766554433',
            'email' => 'andi.wijaya@siswa.sch.id',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ],
        [
            'tipe_peminjam' => 'siswa',
            'id_identitas' => '222310157',
            'nama_lengkap' => 'Siti Aminah',
            'kelas' => 'XI TKJ 2',
            'jurusan' => 'Teknik Komputer Jaringan',
            'nomor_telepon' => '089988776655',
            'email' => 'siti.aminah@siswa.sch.id',
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ]
    ];

    $peminjamStmt = $db->prepare("INSERT INTO sarpras_peminjam (tipe_peminjam, id_identitas, nama_lengkap, kelas, jurusan, nomor_telepon, email, created_at, updated_at) VALUES (:tipe_peminjam, :id_identitas, :nama_lengkap, :kelas, :jurusan, :nomor_telepon, :email, :created_at, :updated_at)");
    foreach ($peminjams as $peminjam) {
        $peminjamStmt->execute($peminjam);
    }
    echo "✓ Seeded sarpras_peminjam successfully\n";

    // 3. Seed sarpras_petugas
    // We map user_id = 2 (which is teacher with username 198910262022211006) as petugas
    $petugas = [
        'user_id' => 2,
        'nip' => '198910262022211006',
        'nama_lengkap' => 'Budi Santoso, S.Pd.',
        'jabatan' => 'Koordinator Sarpras',
        'kontak' => '081234567890',
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s')
    ];

    $petugasStmt = $db->prepare("INSERT INTO sarpras_petugas (user_id, nip, nama_lengkap, jabatan, kontak, created_at, updated_at) VALUES (:user_id, :nip, :nama_lengkap, :jabatan, :kontak, :created_at, :updated_at)");
    $petugasStmt->execute($petugas);
    echo "✓ Seeded sarpras_petugas successfully\n";

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