<?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
    {
        // 1. Update Plans Table
        Schema::table('plans', function (Blueprint $table) {
            if (!Schema::hasColumn('plans', 'type')) {
                $table->string('type')->default('basic'); // 'basic', 'silver'
            }
            if (!Schema::hasColumn('plans', 'duration_months')) {
                $table->integer('duration_months')->default(1);
            }
            if (!Schema::hasColumn('plans', 'price_usd')) {
                $table->decimal('price_usd', 10, 2)->nullable();
            }
            // Renaming price to price_inr if needed, or just adding price_usd
            if (!Schema::hasColumn('plans', 'price_inr')) {
                 $table->decimal('price_inr', 10, 2)->nullable()->after('price');
            }
             if (!Schema::hasColumn('plans', 'product_limit')) {
                $table->integer('product_limit')->default(3);
            }
             if (!Schema::hasColumn('plans', 'limits')) {
                $table->json('limits')->nullable(); // For Inquiry, Contact View toggles
            }
        });

        // 2. Update Sellers Table (Registration Steps & KYC)
        Schema::table('sellers', function (Blueprint $table) {
            if (!Schema::hasColumn('sellers', 'registration_step')) {
                $table->integer('registration_step')->default(1);
            }
            if (!Schema::hasColumn('sellers', 'kyc_status')) {
                $table->enum('kyc_status', ['pending', 'submitted', 'verified', 'rejected'])->default('pending');
            }
            if (!Schema::hasColumn('sellers', 'moq_range_start')) {
                $table->integer('moq_range_start')->nullable();
            }
            if (!Schema::hasColumn('sellers', 'moq_range_end')) {
                $table->integer('moq_range_end')->nullable();
            }
            // GST/PAN/Udyam
            if (!Schema::hasColumn('sellers', 'gst_number')) $table->string('gst_number')->nullable();
            if (!Schema::hasColumn('sellers', 'pan_number')) $table->string('pan_number')->nullable();
            if (!Schema::hasColumn('sellers', 'udyam_number')) $table->string('udyam_number')->nullable();
            
            // Business Type (Array/Multi-select)
             if (!Schema::hasColumn('sellers', 'business_type_json')) {
                $table->json('business_type_json')->nullable();
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('plans', function (Blueprint $table) {
            $table->dropColumn(['type', 'duration_months', 'price_usd', 'price_inr', 'product_limit', 'limits']);
        });

        Schema::table('sellers', function (Blueprint $table) {
            $table->dropColumn([
                'registration_step', 
                'kyc_status', 
                'moq_range_start', 
                'moq_range_end',
                'gst_number',
                'pan_number',
                'udyam_number',
                'business_type_json'
            ]);
        });
    }
};
