<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateProgramStudiTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'fakultas_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'kode_prodi' => [
                'type'       => 'VARCHAR',
                'constraint' => '20',
                'unique'     => true,
            ],
            'nama_prodi' => [
                'type'       => 'VARCHAR',
                'constraint' => '150',
            ],
            'jenjang' => [
                'type'       => 'VARCHAR',
                'constraint' => '20',
            ],
            'akreditasi' => [
                'type'       => 'VARCHAR',
                'constraint' => '5',
                'null'       => true,
            ],
            'kaprodi_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'id_prodi_pddikti' => [
                'type'       => 'VARCHAR',
                'constraint' => '50',
                'null'       => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('fakultas_id', 'fakultas', 'id', 'CASCADE', 'CASCADE');
        // kaprodi_id foreign key will be added later or handled on application level 
        // to avoid circular dependency since dosen table is created after this
        $this->forge->createTable('program_studi');
    }

    public function down()
    {
        $this->forge->dropTable('program_studi');
    }
}
