<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddMissingLearningDeviceColumns extends Migration
{
    public function up()
    {
        // Menambahkan kolom yang ada di DB lokal tapi tidak pernah dibuat migration
        // Setiap kolom dicek dulu dengan fieldExists agar tidak error
        // jika kolom sudah ada (misalnya di lokal yang sudah di-create manual)

        if (!$this->db->fieldExists('chapters', 'learning_devices')) {
            $this->forge->addColumn('learning_devices', [
                'chapters' => [
                    'type'       => 'LONGTEXT',
                    'null'       => true,
                    'default'    => null,
                ],
            ]);
        }

        if (!$this->db->fieldExists('dpl', 'learning_devices')) {
            $this->forge->addColumn('learning_devices', [
                'dpl' => [
                    'type'       => 'LONGTEXT',
                    'null'       => true,
                    'default'    => null,
                ],
            ]);
        }

        if (!$this->db->fieldExists('panca_cinta', 'learning_devices')) {
            $this->forge->addColumn('learning_devices', [
                'panca_cinta' => [
                    'type'       => 'LONGTEXT',
                    'null'       => true,
                    'default'    => null,
                ],
            ]);
        }

        if (!$this->db->fieldExists('kktp_intervals', 'learning_devices')) {
            $this->forge->addColumn('learning_devices', [
                'kktp_intervals' => [
                    'type'       => 'VARCHAR',
                    'constraint' => 255,
                    'null'       => true,
                    'default'    => null,
                ],
            ]);
        }
    }

    public function down()
    {
        $fieldsToDrop = ['kktp_intervals', 'panca_cinta', 'dpl', 'chapters'];
        foreach ($fieldsToDrop as $field) {
            if ($this->db->fieldExists($field, 'learning_devices')) {
                $this->forge->dropColumn('learning_devices', $field);
            }
        }
    }
}
