<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class UpdateSubjectsAddColumns extends Migration
{
    public function up()
    {
        // Tambah kolom phase
        if (!$this->db->fieldExists('phase', 'subjects')) {
            $this->forge->addColumn('subjects', [
                'phase' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 10,
                    'null'       => true,
                    'after'      => 'level',
                ],
            ]);
        }

        // Tambah kolom group
        if (!$this->db->fieldExists('group', 'subjects')) {
            $this->forge->addColumn('subjects', [
                'group' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 50,
                    'null'       => true,
                    'after'      => 'phase',
                ],
            ]);
        }

        // Tambah kolom is_core (wajib nasional vs pilihan)
        if (!$this->db->fieldExists('is_core', 'subjects')) {
            $this->forge->addColumn('subjects', [
                'is_core' => [
                    'type'       => 'TINYINT',
                    'constraint' => 1,
                    'default'    => 1,
                    'after'      => 'group',
                ],
            ]);
        }
    }

    public function down()
    {
        if ($this->db->fieldExists('phase', 'subjects')) {
            $this->forge->dropColumn('subjects', 'phase');
        }
        if ($this->db->fieldExists('group', 'subjects')) {
            $this->forge->dropColumn('subjects', 'group');
        }
        if ($this->db->fieldExists('is_core', 'subjects')) {
            $this->forge->dropColumn('subjects', 'is_core');
        }
    }
}