<?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) {
            if (!Schema::hasColumn('sellers', 'city')) $table->string('city')->nullable()->after('country');
            if (!Schema::hasColumn('sellers', 'pincode')) $table->string('pincode')->nullable()->after('city');
            if (!Schema::hasColumn('sellers', 'address')) $table->text('address')->nullable()->after('pincode');
            if (!Schema::hasColumn('sellers', 'pan_number')) $table->string('pan_number')->nullable()->after('gst');
            if (!Schema::hasColumn('sellers', 'udyam_number')) $table->string('udyam_number')->nullable()->after('pan_number');
            if (!Schema::hasColumn('sellers', 'business_type')) $table->json('business_type')->nullable()->after('company_name');
            if (!Schema::hasColumn('sellers', 'moq_range_start')) $table->integer('moq_range_start')->default(10)->after('moq');
            if (!Schema::hasColumn('sellers', 'moq_range_end')) $table->integer('moq_range_end')->default(150)->after('moq_range_start');
            
            if (!Schema::hasColumn('sellers', 'category_id')) {
                $table->foreignId('category_id')->nullable()->after('user_id')->constrained('categories')->nullOnDelete();
            }

            if (!Schema::hasColumn('sellers', 'gst_doc')) $table->string('gst_doc')->nullable();
            if (!Schema::hasColumn('sellers', 'pan_doc')) $table->string('pan_doc')->nullable();
            if (!Schema::hasColumn('sellers', 'udyam_doc')) $table->string('udyam_doc')->nullable();
            if (!Schema::hasColumn('sellers', 'kyc_status')) $table->string('kyc_status')->default('pending')->after('is_verified');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('sellers', function (Blueprint $table) {
            $table->dropForeign(['category_id']);
            $table->dropColumn([
                'city', 'pincode', 'address', 
                'pan_number', 'udyam_number', 
                'business_type', 
                'moq_range_start', 'moq_range_end', 
                'category_id',
                'gst_doc', 'pan_doc', 'udyam_doc', 'kyc_status'
            ]);
        });
    }
};
