<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::table('skumaster', function (Blueprint $table) {

            // Add foreign keys
            $table->foreignId('stock_item_id')
                ->after('id')
                ->constrained('stock_items')
                ->cascadeOnDelete();

            $table->foreignId('category_id')
                ->after('stock_item_id')
                ->constrained('categories')
                ->cascadeOnDelete();

            // Drop old varchar columns
            $table->dropColumn(['item_name', 'category']);
        });
    }

    public function down()
    {
        Schema::table('skumaster', function (Blueprint $table) {

            $table->string('item_name');
            $table->string('category');

            $table->dropForeign(['stock_item_id']);
            $table->dropForeign(['category_id']);
            $table->dropColumn(['stock_item_id', 'category_id']);
        });
    }
};
