<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('vendors', function (Blueprint $table) {
            // Add store_name if not exists (alias for shop_name)
            if (!Schema::hasColumn('vendors', 'store_name')) {
                $table->string('store_name')->nullable()->after('user_id');
            }
            
            // Add store_description if not exists (alias for description)
            if (!Schema::hasColumn('vendors', 'store_description')) {
                $table->text('store_description')->nullable()->after('store_name');
            }
            
            if (!Schema::hasColumn('vendors', 'business_category')) {
                $table->string('business_category')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'business_email')) {
                $table->string('business_email')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'business_phone')) {
                $table->string('business_phone')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'city')) {
                $table->string('city')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'state')) {
                $table->string('state')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'country')) {
                $table->string('country')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'postal_code')) {
                $table->string('postal_code')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'website')) {
                $table->string('website')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'tax_id')) {
                $table->string('tax_id')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'trade_license_path')) {
                $table->string('trade_license_path')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'business_registration_path')) {
                $table->string('business_registration_path')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'id_document_path')) {
                $table->string('id_document_path')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'onboarding_step')) {
                $table->tinyInteger('onboarding_step')->default(0);
            }
            if (!Schema::hasColumn('vendors', 'onboarding_completed_at')) {
                $table->timestamp('onboarding_completed_at')->nullable();
            }
            if (!Schema::hasColumn('vendors', 'rejection_reason')) {
                $table->text('rejection_reason')->nullable();
            }
        });
    }

    public function down(): void
    {
        Schema::table('vendors', function (Blueprint $table) {
            $columns = [
                'store_name', 'store_description', 'business_category', 
                'business_email', 'business_phone', 'city', 'state', 'country', 
                'postal_code', 'website', 'tax_id', 'trade_license_path', 
                'business_registration_path', 'id_document_path', 'onboarding_step', 
                'onboarding_completed_at', 'rejection_reason'
            ];
            
            foreach ($columns as $column) {
                if (Schema::hasColumn('vendors', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
