<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddColumnsToTbReseller extends Migration
{
    public function up(): void
    {
        $fields = $this->db->getFieldNames('tb_reseller');

        $adds = [];

        if (!in_array('id_produk_terkait_reseller', $fields)) {
            $adds[] = "ADD COLUMN `id_produk_terkait_reseller` VARCHAR(255) NULL DEFAULT NULL AFTER `harga_default_reseller`";
        }

        if (!in_array('link_reseller', $fields)) {
            $adds[] = "ADD COLUMN `link_reseller` VARCHAR(500) NULL DEFAULT NULL AFTER `id_produk_terkait_reseller`";
        }

        if (!in_array('updated_at', $fields)) {
            $adds[] = "ADD COLUMN `updated_at` DATETIME NULL DEFAULT NULL AFTER `created_at`";
        }

        if (!empty($adds)) {
            $this->db->query("ALTER TABLE `tb_reseller` " . implode(', ', $adds));
        }
    }

    public function down(): void
    {
        $fields = $this->db->getFieldNames('tb_reseller');

        $drops = [];

        if (in_array('id_produk_terkait_reseller', $fields)) {
            $drops[] = "DROP COLUMN `id_produk_terkait_reseller`";
        }

        if (in_array('link_reseller', $fields)) {
            $drops[] = "DROP COLUMN `link_reseller`";
        }

        if (in_array('updated_at', $fields)) {
            $drops[] = "DROP COLUMN `updated_at`";
        }

        if (!empty($drops)) {
            $this->db->query("ALTER TABLE `tb_reseller` " . implode(', ', $drops));
        }
    }
}