<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddDapodikIdToStudentsTable extends Migration
{
    public function up()
    {
        // Add dapodik_id column (nullable, unique) to students
        $this->forge->addColumn('students', [
            'dapodik_id' => [
                'type'       => 'VARCHAR',
                'constraint' => 64,
                'null'       => true,
                'after'      => 'id',
            ],
        ]);

        // Add unique index on dapodik_id (for upsert)
        $this->db->query('CREATE UNIQUE INDEX students_dapodik_id_unique ON ' . $this->db->prefixTable('students') . ' (dapodik_id)');
    }

    public function down()
    {
        // Drop unique index and column
        $this->db->query('DROP INDEX students_dapodik_id_unique ON ' . $this->db->prefixTable('students'));
        $this->forge->dropColumn('students', 'dapodik_id');
    }
}

