<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddNipToDokumen extends Migration
{
    public function up()
    {
        $this->forge->addColumn('dokumen', [
            'nip' => [
                'type' => 'VARCHAR',
                'constraint' => 20,
                'null' => true,
                'after' => 'pengajuan_id'
            ]
        ]);
        $this->db->query('CREATE INDEX idx_dokumen_nip ON dokumen(nip)');

        // Backfill NIP from pengajuan table
        $this->db->query("
            UPDATE dokumen 
            JOIN pengajuan ON dokumen.pengajuan_id = pengajuan.id 
            SET dokumen.nip = pengajuan.target_nip
            WHERE dokumen.nip IS NULL
        ");
    }

    public function down()
    {
        $this->forge->dropColumn('dokumen', 'nip');
    }
}
