<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateStudentGraduationsTable extends Migration
{
    public function up()
    {
        // Tabel student_graduations — menyimpan status kelulusan per siswa
        // Tabel ini sudah ada di database lokal (dibuat via SQL manual)
        // tetapi belum pernah dibuat migration-nya sehingga tidak ada di online

        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'student_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'academic_year_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
            ],
            'kelulusan_status' => [
                'type'       => "ENUM('LULUS','TIDAK_LULUS','BELUM_DITENTUKAN')",
                'null'       => false,
                'default'    => 'BELUM_DITENTUKAN',
            ],
            'decided_at' => [
                'type'       => 'DATE',
                'null'       => true,
                'default'    => null,
            ],
            'decided_by' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => true,
                'default'    => null,
            ],
            'skl_file_path' => [
                'type'       => 'VARCHAR',
                'constraint' => 500,
                'null'       => true,
                'default'    => null,
            ],
            'skl_uploaded_at' => [
                'type'       => 'DATETIME',
                'null'       => true,
                'default'    => null,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'deleted_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('student_id', 'students', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('academic_year_id', 'academic_years', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addUniqueKey(['student_id', 'academic_year_id'], 'student_graduations_student_year_unique');
        $this->forge->createTable('student_graduations', true); // true = IF NOT EXISTS
    }

    public function down()
    {
        $this->forge->dropTable('student_graduations', true); // true = IF EXISTS
    }
}
