<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddShopCategories extends Migration
{
    public function up()
    {
        if (!$this->db->tableExists('rewarder_shop_categories')) {
            $this->forge->addField([
                'id' => [
                    'type' => 'INT',
                    'constraint' => 11,
                    'unsigned' => true,
                    'auto_increment' => true,
                ],
                'name' => [
                    'type' => 'VARCHAR',
                    'constraint' => 120,
                ],
                'status' => [
                    'type' => 'TINYINT',
                    'constraint' => 1,
                    'default' => 1,
                ],
                'created_at' => [
                    'type' => 'DATETIME',
                    'null' => true,
                ],
                'updated_at' => [
                    'type' => 'DATETIME',
                    'null' => true,
                ],
            ]);
            $this->forge->addKey('id', true);
            $this->forge->addUniqueKey('name');
            $this->forge->createTable('rewarder_shop_categories', true);
        }

        if (!$this->db->fieldExists('category_id', 'rewarder_shops')) {
            $this->forge->addColumn('rewarder_shops', [
                'category_id' => [
                    'type' => 'INT',
                    'constraint' => 11,
                    'unsigned' => true,
                    'null' => true,
                    'after' => 'city_id',
                ],
            ]);
        }
    }

    public function down()
    {
        if ($this->db->fieldExists('category_id', 'rewarder_shops')) {
            $this->forge->dropColumn('rewarder_shops', 'category_id');
        }

        if ($this->db->tableExists('rewarder_shop_categories')) {
            $this->forge->dropTable('rewarder_shop_categories', true);
        }
    }
}

