<?php

namespace App\Database\Migrations\Elearning;

use CodeIgniter\Database\Migration;

class Absensi extends Migration
{
    public function up()
    {
        $fields = [
            'siswa_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'latIn' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'lonIn' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'latOut' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'lonOut' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'methodIn' => ['type' => 'enum', 'constraint' => "'qr','map','rf'", 'null' => true, 'default' => null],
            'methodOut' => ['type' => 'enum', 'constraint' => "'qr','map','rf'", 'null' => true, 'default' => null],
            'status' => ['type' => 'enum', 'constraint' => "'1','2','3','4'", 'default' => '1', 'comment' => "1=>'Hadir',2=>'Alpa',3=>'Sakit',4=>'Izin'"],
            'suhuIn' => ['type' => 'double', 'null' => true, 'constraint' => '5,2'],
            'suhuOut' => ['type' => 'double', 'null' => true, 'constraint' => '5,2'],
            'timeIn' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => null],
            'timeOut' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => null],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addForeignKey('siswa_id', 'm_siswa', 'id', false, 'CASCADE');
        $this->forge->createTable('daily_presence_siswa', true);

        $fields = [
            'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'latIn' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'lonIn' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'latOut' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'lonOut' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'methodIn' => ['type' => 'enum', 'constraint' => "'qr','map','rf'", 'null' => true, 'default' => null],
            'methodOut' => ['type' => 'enum', 'constraint' => "'qr','map','rf'", 'null' => true, 'default' => null],
            'status' => ['type' => 'enum', 'constraint' => "'1','2','3','4'", 'default' => '1', 'comment' => "1=>'Hadir',2=>'Alpa',3=>'Sakit',4=>'Izin'"],
            'suhuIn' => ['type' => 'double', 'null' => true, 'constraint' => '5,2'],
            'suhuOut' => ['type' => 'double', 'null' => true, 'constraint' => '5,2'],
            'timeIn' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => null],
            'timeOut' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => null],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addForeignKey('user_id', 'users', 'id', false, 'CASCADE');
        $this->forge->createTable('daily_presence_guru', true);

        $fields = [
            'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'latIn' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'lonIn' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'latOut' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'lonOut' => ['type' => 'float', 'constraint' => '10,6', 'null' => true],
            'methodIn' => ['type' => 'enum', 'constraint' => "'qr','map','rf'", 'null' => true, 'default' => null],
            'methodOut' => ['type' => 'enum', 'constraint' => "'qr','map','rf'", 'null' => true, 'default' => null],
            'status' => ['type' => 'enum', 'constraint' => "'1','2','3','4'", 'default' => '1', 'comment' => "1=>'Hadir',2=>'Alpa',3=>'Sakit',4=>'Izin'"],
            'suhuIn' => ['type' => 'double', 'null' => true, 'constraint' => '5,2'],
            'suhuOut' => ['type' => 'double', 'null' => true, 'constraint' => '5,2'],
            'timeIn' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => null],
            'timeOut' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'default' => null],
            'created_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'updated_at' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addForeignKey('user_id', 'users', 'id', false, 'CASCADE');
        $this->forge->createTable('daily_presence_karyawan', true);
    }

    public function down()
    {
        // drop constraints first to prevent errors
        if ($this->db->DBDriver != 'SQLite3') {
            $this->forge->dropForeignKey('daily_presence_siswa', 'daily_presence_siswa_siswa_id_foreign');
            $this->forge->dropForeignKey('daily_presence_guru', 'daily_presence_guru_user_id_foreign');
            $this->forge->dropForeignKey('daily_presence_karyawan', 'daily_presence_karyawan_user_id_foreign');
        }
        $this->forge->dropTable('daily_presence_siswa', true);
        $this->forge->dropTable('daily_presence_guru', true);
        $this->forge->dropTable('daily_presence_karyawan', true);
    }
}
