<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddNewStudentFields extends Migration
{
    public function up()
    {
        // Add family_card_no (No KK)
        if (!$this->db->fieldExists('family_card_no', 'students')) {
            $this->forge->addColumn('students', [
                'family_card_no' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 30,
                    'null'       => true,
                    'default'    => null,
                    'after'      => 'nik'
                ],
            ]);
        }

        // Add family_head_name (Nama Kepala Keluarga)
        if (!$this->db->fieldExists('family_head_name', 'students')) {
            $this->forge->addColumn('students', [
                'family_head_name' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 255,
                    'null'       => true,
                    'default'    => null,
                    'after'      => 'family_card_no'
                ],
            ]);
        }

        // Add kip_no (No KIP)
        if (!$this->db->fieldExists('kip_no', 'students')) {
            $this->forge->addColumn('students', [
                'kip_no' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 50,
                    'null'       => true,
                    'default'    => null,
                    'after'      => 'family_head_name'
                ],
            ]);
        }

        // Add graduation_certificate_no (No Seri Ijazah)
        if (!$this->db->fieldExists('graduation_certificate_no', 'students')) {
            $this->forge->addColumn('students', [
                'graduation_certificate_no' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 100,
                    'null'       => true,
                    'default'    => null,
                    'after'      => 'origin_school'
                ],
            ]);
        }
    }

    public function down()
    {
        $fieldsToDrop = ['family_card_no', 'family_head_name', 'kip_no', 'graduation_certificate_no'];
        foreach ($fieldsToDrop as $field) {
            if ($this->db->fieldExists($field, 'students')) {
                $this->forge->dropColumn('students', $field);
            }
        }
    }
}
