<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class SipediSeeder extends Seeder
{
    public function run()
    {
        // Truncate tables to clean up before seeding (turn off FK checks first)
        $this->db->query('SET FOREIGN_KEY_CHECKS = 0;');
        $this->db->table('log_gerbang')->truncate();
        $this->db->table('perizinan')->truncate();
        $this->db->table('santri')->truncate();
        $this->db->table('users')->truncate();
        $this->db->query('SET FOREIGN_KEY_CHECKS = 1;');

        // 1. Seed Users
        $userData = [
            [
                'id'           => 1,
                'username'     => 'admin',
                'password'     => password_hash('admin123', PASSWORD_BCRYPT),
                'role'         => 'admin',
                'nama_lengkap' => 'Administrator SIPEDI',
            ],
            [
                'id'           => 2,
                'username'     => 'kesantrian',
                'password'     => password_hash('kesantrian123', PASSWORD_BCRYPT),
                'role'         => 'kesantrian',
                'nama_lengkap' => 'Ustadz Abdullah (Kesantrian)',
            ],
            [
                'id'           => 3,
                'username'     => 'satpam',
                'password'     => password_hash('satpam123', PASSWORD_BCRYPT),
                'role'         => 'satpam',
                'nama_lengkap' => 'Pak Joko (Satpam Gerbang)',
            ],
            [
                'id'           => 4,
                'username'     => 'wali',
                'password'     => password_hash('wali123', PASSWORD_BCRYPT),
                'role'         => 'wali',
                'nama_lengkap' => 'Bapak Budi Santoso (Wali)',
            ],
            [
                'id'           => 5,
                'username'     => 'wali2',
                'password'     => password_hash('wali123', PASSWORD_BCRYPT),
                'role'         => 'wali',
                'nama_lengkap' => 'Bapak Ahmad Fauzi (Wali 2)',
            ],
        ];

        $this->db->table('users')->insertBatch($userData);

        // 2. Seed Santri
        $santriData = [
            [
                'id'      => 1,
                'nis'     => '1001',
                'nama'    => 'Ahmad Rayhan',
                'kelas'   => '10A',
                'asrama'  => 'Abu Bakar',
                'wali_id' => 4,
            ],
            [
                'id'      => 2,
                'nis'     => '1002',
                'nama'    => 'Muhammad Zaky',
                'kelas'   => '11B',
                'asrama'  => 'Umar bin Khattab',
                'wali_id' => 4,
            ],
            [
                'id'      => 3,
                'nis'     => '1003',
                'nama'    => 'Fathir Rizky',
                'kelas'   => '12C',
                'asrama'  => 'Usman bin Affan',
                'wali_id' => 5,
            ],
        ];

        $this->db->table('santri')->insertBatch($santriData);

        // 3. Seed some dummy perizinan for Ahmad Rayhan
        $perizinanData = [
            [
                'santri_id'     => 1,
                'jenis_izin'    => 'Keluar (Belanja kebutuhan)',
                'waktu_keluar'  => date('Y-m-d H:i:s', strtotime('-1 day')),
                'waktu_kembali' => date('Y-m-d H:i:s', strtotime('-1 day + 3 hours')),
                'alasan'        => 'Membeli perlengkapan mandi di minimarket terdekat.',
                'status'        => 'done',
                'token_izin'    => 'IZN-' . strtoupper(bin2hex(random_bytes(6))),
                'created_by'    => 4,
                'created_at'    => date('Y-m-d H:i:s', strtotime('-1 day')),
            ],
            [
                'santri_id'     => 1,
                'jenis_izin'    => 'Pulang (Acara Keluarga)',
                'waktu_keluar'  => date('Y-m-d H:i:s', strtotime('+1 day')),
                'waktu_kembali' => date('Y-m-d H:i:s', strtotime('+3 days')),
                'alasan'        => 'Menghadiri pernikahan paman kandung.',
                'status'        => 'approved',
                'token_izin'    => 'IZN-' . strtoupper(bin2hex(random_bytes(6))),
                'created_by'    => 4,
                'created_at'    => date('Y-m-d H:i:s'),
            ],
            [
                'santri_id'     => 2,
                'jenis_izin'    => 'Keluar (Berobat)',
                'waktu_keluar'  => date('Y-m-d H:i:s'),
                'waktu_kembali' => date('Y-m-d H:i:s', strtotime('+4 hours')),
                'alasan'        => 'Pemeriksaan gigi ke klinik pratama.',
                'status'        => 'active', // Sedang di luar
                'token_izin'    => 'IZN-' . strtoupper(bin2hex(random_bytes(6))),
                'created_by'    => 4,
                'created_at'    => date('Y-m-d H:i:s'),
            ]
        ];

        $this->db->table('perizinan')->insertBatch($perizinanData);
    }
}
