<?php

namespace App\Database\Migrations\Approvals;

use CodeIgniter\Database\Migration;

class Kehadiran extends Migration
{
    public function up()
    {
        $fields = [
            'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'status' => ['type' => 'enum', 'constraint' => "'1','2','3','4'", 'default' => '1', 'comment' => "1=>'Menunggu',2=>'Disetujui',3=>'Ditolak',4=>'Dibatalkan'"],
            'approval_description' => ['type' => 'text', 'null' => true],
            'presence_status' => ['type' => 'enum', 'constraint' => "'1','2','3','4'", 'default' => '4', 'comment' => "1=>'Hadir',2=>'Alpa',3=>'Sakit',4=>'Izin'"],
            'approver_description' => ['type' => 'text', 'null' => true],
            'tanggal' => ['type' => 'date'],
            'edited_by' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('user_id', 'users', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('edited_by', 'users', 'id', false, 'SET NULL');
        $this->forge->createTable('approvals_presence_guru', true);

        $fields = [
            'id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'siswa_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'status' => ['type' => 'enum', 'constraint' => "'1','2','3','4'", 'default' => '1', 'comment' => "1=>'Menunggu',2=>'Disetujui',3=>'Ditolak',4=>'Dibatalkan'"],
            'approval_description' => ['type' => 'text', 'null' => true],
            'presence_status' => ['type' => 'enum', 'constraint' => "'1','2','3','4'", 'default' => '4', 'comment' => "1=>'Hadir',2=>'Alpa',3=>'Sakit',4=>'Izin'"],
            'approver_description' => ['type' => 'text', 'null' => true],
            'tanggal' => ['type' => 'date'],
            'edited_by' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('siswa_id', 'm_siswa', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('edited_by', 'users', 'id', false, 'SET NULL');
        $this->forge->createTable('approvals_presence_siswa', true);
    }

    public function down()
    {
        // drop constraints first to prevent errors
        if ($this->db->DBDriver != 'SQLite3') {
            $this->forge->dropForeignKey('approvals_presence_guru', 'approvals_presence_guru_user_id_foreign');
            $this->forge->dropForeignKey('approvals_presence_guru', 'approvals_presence_guru_edited_by_foreign');
            $this->forge->dropForeignKey('approvals_presence_siswa', 'approvals_presence_siswa_siswa_id_foreign');
            $this->forge->dropForeignKey('approvals_presence_siswa', 'approvals_presence_siswa_edited_by_foreign');
        }
        $this->forge->dropTable('approvals_presence_guru', true);
        $this->forge->dropTable('approvals_presence_siswa', true);
    }
}
