<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('sellers', function (Blueprint $table) {
            // Fix: Add category_id if missing
            if (!Schema::hasColumn('sellers', 'category_id')) {
                $table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
            }

            // Add moq_quantity if missing
            if (!Schema::hasColumn('sellers', 'moq_quantity')) {
                $table->integer('moq_quantity')->nullable(); // Removed 'after' to avoid errors
            }

            // Add moq_unit_id if missing
            if (!Schema::hasColumn('sellers', 'moq_unit_id')) {
                $table->foreignId('moq_unit_id')->nullable()->constrained('units')->nullOnDelete();
            }
        });

        // Drop old 'moq' column if exists
        if (Schema::hasColumn('sellers', 'moq')) {
            Schema::table('sellers', function (Blueprint $table) {
                $table->dropColumn('moq');
            });
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('sellers', function (Blueprint $table) {
            $table->string('moq')->nullable()->after('category_id');
            $table->dropForeign(['moq_unit_id']);
            $table->dropColumn(['moq_quantity', 'moq_unit_id']);
        });
    }
};
