<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddKrsColumnsToPesertaKelas extends Migration
{
    public function up()
    {
        // 1. Add dosen_pa_id to mahasiswa table
        $this->forge->addColumn('mahasiswa', [
            'dosen_pa_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'after'      => 'prodi_id'
            ],
        ]);

        // 2. Create krs table
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'mahasiswa_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'tahun_akademik_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'status' => [
                'type'       => 'ENUM',
                'constraint' => ['Draft', 'Menunggu Persetujuan', 'Disetujui', 'Ditolak'],
                'default'    => 'Draft',
            ],
            'catatan' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->createTable('krs');

        // 3. Add krs_id to peserta_kelas
        $this->forge->addColumn('peserta_kelas', [
            'krs_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'after'      => 'id'
            ],
        ]);
    }

    public function down()
    {
        $this->forge->dropColumn('peserta_kelas', 'krs_id');
        $this->forge->dropTable('krs');
        $this->forge->dropColumn('mahasiswa', 'dosen_pa_id');
    }
}
