<?php

namespace App\Database\Migrations\Master;

use CodeIgniter\Database\Migration;

class Siswa extends Migration
{
    public function up()
    {
        $fields = [
            'id'   => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'nomor_induk' => ['type' => 'char', 'constraint' => 128],
            'rfid' => ['type' => 'char', 'constraint' => 64, 'null' => true],
            'nama' => ['type' => 'varchar', 'constraint' => 255],
            'email' => ['type' => 'varchar', 'constraint' => 255, 'null' => true],
            'phone' => ['type' => 'char', 'constraint' => 20, 'null' => true],
            'agama' => ['type' => 'enum', 'default' => 'islam', 'constraint' => "'islam','katolik','protestan','hindu','buddha','konghucu'"],
            'jenis_kelamin' => ['type' => 'enum', 'constraint' => "'L','P'", null => true,],
            'tempat_lahir'  => ['type' => 'varchar', 'constraint' => 255, null => true],
            'tanggal_lahir' => ['type' => 'date', null => true,],
            'alamat'        => ['type' => 'text', null => true,],
            'password_hash' => ['type' => 'blob'],
            'attendance_solution_id'           => ['type' => 'CHAR', 'constraint' => 128, 'default' => null, 'null' => true, 'unique' => true],
            'kelas_id'           => ['type' => 'INT', 'constraint' => 11, 'default' => null, 'null' => true, 'unsigned' => true],
            'ruang_id'           => ['type' => 'INT', 'constraint' => 11, 'default' => null, 'null' => true, 'unsigned' => true],
            'photo'             => ['type' => 'VARCHAR', 'constraint' =>    255, 'default' => 'profile-photo.png'],
            'status'        => ['type' => 'enum', 'constraint' => ['1', '0'], 'default' => '1'],
            'created_at'               => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at'               => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'active_at'               => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('nomor_induk', true);
        $this->forge->addForeignKey('kelas_id', 'm_kelas', 'id', false, 'SET NULL');
        $this->forge->addForeignKey('ruang_id', 'm_ruangujian', 'id', false, 'SET NULL');
        $this->forge->createTable('m_siswa', true);
    }

    public function down()
    {
        if ($this->db->DBDriver != 'SQLite3') {
            $this->forge->dropForeignKey('m_siswa', 'm_siswa_kelas_id_foreign');
            $this->forge->dropForeignKey('m_siswa', 'm_siswa_ruang_id_foreign');
        }
        $this->forge->dropTable('m_siswa', true);
    }
}
