<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_fix_promo_report_tracking extends CI_Migration
{
    public function up()
    {
        $idColumn = $this->db->query("SHOW COLUMNS FROM `report` LIKE 'id'")->row_array();
        if (!empty($idColumn) && stripos((string) ($idColumn['Extra'] ?? ''), 'auto_increment') === false) {
            $this->db->query('ALTER TABLE `report` MODIFY `id` INT(11) NOT NULL AUTO_INCREMENT');
        }
        if (!$this->db->field_exists('id_konsumen', 'report')) $this->dbforge->add_column('report', ['id_konsumen'=>['type'=>'INT','constraint'=>11,'null'=>true,'after'=>'id_kirim']]);
        if (!$this->db->field_exists('state', 'report')) $this->dbforge->add_column('report', ['state'=>['type'=>'VARCHAR','constraint'=>30,'null'=>true,'after'=>'status']]);
        if (!$this->db->field_exists('stateid', 'report')) $this->dbforge->add_column('report', ['stateid'=>['type'=>'VARCHAR','constraint'=>100,'null'=>true,'after'=>'state']]);
        if (!$this->db->field_exists('create_date', 'report')) $this->dbforge->add_column('report', ['create_date'=>['type'=>'DATETIME','null'=>true,'after'=>'stateid']]);
        else $this->db->query('ALTER TABLE `report` MODIFY `create_date` DATETIME NULL');
    }

    public function down() {}
}
