<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddCategoryAndItemTypeToInventory extends Migration
{
    public function up(): void
    {
        $this->forge->addColumn('inventory', [
            'item_type' => [
                'type'       => 'ENUM',
                'constraint' => ['fuel', 'gas_size', 'shop'],
                'default'    => 'fuel',
                'null'       => true,
                'after'      => 'provider_id',
            ],
            'category_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
                'after'      => 'item_type',
            ],
        ]);
        $this->db->query('ALTER TABLE inventory ADD CONSTRAINT fk_inventory_category FOREIGN KEY (category_id) REFERENCES shop_categories(id) ON DELETE SET NULL ON UPDATE CASCADE');
    }

    public function down(): void
    {
        $this->db->query('ALTER TABLE inventory DROP FOREIGN KEY fk_inventory_category');
        $this->forge->dropColumn('inventory', ['item_type', 'category_id']);
    }
}
