<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddSortOrderToSchoolSubjects extends Migration
{
    public function up()
    {
        if (!$this->db->fieldExists('sort_order', 'school_subjects')) {
            $this->forge->addColumn('school_subjects', [
                'sort_order' => [
                    'type'       => 'INT',
                    'constraint' => 11,
                    'default'    => 0,
                    'after'      => 'is_active',
                ],
            ]);
        }
        
        // Set sort_order awal berdasarkan id
        $this->db->query("UPDATE school_subjects SET sort_order = id");
    }

    public function down()
    {
        if ($this->db->fieldExists('sort_order', 'school_subjects')) {
            $this->forge->dropColumn('school_subjects', 'sort_order');
        }
    }
}