<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateGraduationSettingsTable extends Migration
{
    public function up()
    {
        $this->db->disableForeignKeyChecks();

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'academic_year_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => false,
                'unique'     => true,
            ],
            'final_grade_id' => [
                'type'       => 'BIGINT',
                'unsigned'   => true,
                'null'       => true,
            ],
            'announcement_datetime' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'is_active' => [
                'type'    => 'BOOLEAN',
                'default' => false,
            ],
            'custom_message' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('academic_year_id', 'academic_years', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('final_grade_id', 'grades', 'id', 'SET NULL', 'CASCADE');

        $this->forge->createTable('graduation_settings', true);

        $this->db->enableForeignKeyChecks();
    }

    public function down()
    {
        $this->forge->dropForeignKey('graduation_settings', 'graduation_settings_academic_year_id_foreign');
        $this->forge->dropForeignKey('graduation_settings', 'graduation_settings_final_grade_id_foreign');
        $this->forge->dropTable('graduation_settings');
    }
}
