<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddBobotByAdminToAssessments extends Migration
{
    public function up()
    {
        // Cek dulu apakah kolom sudah ada (idempotent)
        $db = \Config\Database::connect();
        $cols = $db->query("SHOW COLUMNS FROM assessments LIKE 'bobot_by_admin'")->getResultArray();
        if (!empty($cols)) {
            return; // Sudah ada, skip
        }

        $fields = [
            'bobot_by_admin' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'null'       => false,
                'default'    => 0,
                'after'      => 'has_essay',
            ],
            'admin_bobot_pg' => [
                'type'       => 'INT',
                'null'       => false,
                'default'    => 0,
                'after'      => 'bobot_by_admin',
            ],
            'admin_bobot_bs' => [
                'type'       => 'INT',
                'null'       => false,
                'default'    => 0,
                'after'      => 'admin_bobot_pg',
            ],
            'admin_bobot_isian' => [
                'type'       => 'INT',
                'null'       => false,
                'default'    => 0,
                'after'      => 'admin_bobot_bs',
            ],
            'admin_bobot_essay' => [
                'type'       => 'INT',
                'null'       => false,
                'default'    => 0,
                'after'      => 'admin_bobot_isian',
            ],
        ];

        $this->forge->addColumn('assessments', $fields);
    }

    public function down()
    {
        $this->forge->dropColumn('assessments', [
            'bobot_by_admin',
            'admin_bobot_pg',
            'admin_bobot_bs',
            'admin_bobot_isian',
            'admin_bobot_essay',
        ]);
    }
}
