<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddKolomPengaturan extends Migration
{
    public function up()
    {
        $fields = [];

        if (!$this->db->fieldExists('deskripsi', 'pengaturan')) {
            $fields['deskripsi'] = [
                'type' => 'TEXT',
                'null' => true,
            ];
        }

        if (!$this->db->fieldExists('alamat', 'pengaturan')) {
            $fields['alamat'] = [
                'type' => 'TEXT',
                'null' => true,
            ];
        }

        if (!$this->db->fieldExists('telepon', 'pengaturan')) {
            $fields['telepon'] = [
                'type'       => 'VARCHAR',
                'constraint' => 25,
                'null'       => true,
            ];
        }

        if (!$this->db->fieldExists('email', 'pengaturan')) {
            $fields['email'] = [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => true,
            ];
        }

        if (!$this->db->fieldExists('website', 'pengaturan')) {
            $fields['website'] = [
                'type'       => 'VARCHAR',
                'constraint' => 150,
                'null'       => true,
            ];
        }

        if (!$this->db->fieldExists('logo', 'pengaturan')) {
            $fields['logo'] = [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
            ];
        }

        if (!$this->db->fieldExists('favicon', 'pengaturan')) {
            $fields['favicon'] = [
                'type'       => 'VARCHAR',
                'constraint' => 255,
                'null'       => true,
            ];
        }

        if (!empty($fields)) {
            $this->forge->addColumn('pengaturan', $fields);
        }
    }

    public function down()
    {
        $columns = [
            'deskripsi',
            'alamat',
            'telepon',
            'email',
            'website',
            'logo',
            'favicon',
        ];

        foreach ($columns as $column) {
            if ($this->db->fieldExists($column, 'pengaturan')) {
                $this->forge->dropColumn('pengaturan', $column);
            }
        }
    }
}