<?php

namespace App\Database\Migrations\Panel;

use CodeIgniter\Database\Migration;

class Auth extends Migration
{
    public function up()
    {
        /*
         * Users
         */
        $fields = [
            'id'               => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'nomor_induk'      => ['type' => 'CHAR', 'constraint' => 30, 'null' => true, 'unqiue' => true],
            'rfid'             => ['type' => 'CHAR', 'constraint' => 64, 'null' => true],
            'username'         => ['type' => 'CHAR', 'constraint' => 128],
            'fullname'         => ['type' => 'VARCHAR', 'constraint' => 255],
            'attendance_solution_id' => ['type' => 'CHAR', 'constraint' => 128, 'null' => true, 'unique' => true],
            'kelas_id'           => ['type' => 'INT', 'constraint' => 11, 'null' => true, 'unsigned' => true],
            'cover'         => ['type' => 'VARCHAR', 'constraint' => 255, 'default' => 'profile-cover.jpg'],
            'photo'         => ['type' => 'VARCHAR', 'constraint' => 255, 'default' => 'profile-photo.png'],
            'about_me'         => ['type' => 'TEXT', 'null' => true],
            'password_hash'    => ['type' => 'VARCHAR', 'constraint' => 255],
            'status'           =>  ['type' => 'enum', 'constraint' => ['1', '0'], 'default' => '1'],
            'phone' => ['type' => 'CHAR', 'constraint' => 20, 'null' => true],
            '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],
            'deleted_at' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
        ];
        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('username');
        $this->forge->addForeignKey('kelas_id', 'm_kelas', 'id', false, 'SET NULL');
        $this->forge->createTable('users', true);

        /*
         * Role Table
         */
        $fields = [
            'id'          => ['type' => 'int', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'name'        => ['type' => 'varchar', 'constraint' => 128],
            'description' => ['type' => 'varchar', 'constraint' => 255],
        ];

        $this->forge->addField($fields);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('name');
        $this->forge->createTable('roles', true);

        /*
         * Role User Table
         */
        $fields = [
            'role_id'      => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
            'user_id' => ['type' => 'int', 'constraint' => 11, 'unsigned' => true],
        ];

        $this->forge->addField($fields);
        $this->forge->addForeignKey('role_id', 'roles', 'id', false, 'CASCADE');
        $this->forge->addForeignKey('user_id', 'users', 'id', false, 'CASCADE');
        $this->forge->createTable('users_roles', true);
    }

    public function down()
    {
        // drop constraints first to prevent errors
        if ($this->db->DBDriver != 'SQLite3') {
            $this->forge->dropForeignKey('users', 'users_kelas_id_foreign');
            $this->forge->dropForeignKey('users_roles', 'users_roles_role_id_foreign');
            $this->forge->dropForeignKey('users_roles', 'users_roles_user_id_foreign');
        }
        $this->forge->dropTable('users', true);
        $this->forge->dropTable('roles', true);
        $this->forge->dropTable('users_roles', true);
    }
}
