<?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 new columns for marketplace functionality
            if (!Schema::hasColumn('vendors', 'slug')) {
                $table->string('slug')->unique()->after('business_name');
            }

            if (!Schema::hasColumn('vendors', 'contact_person')) {
                $table->string('contact_person')->after('slug');
            }

            if (!Schema::hasColumn('vendors', 'address')) {
                $table->text('address')->after('phone');
            }

            if (!Schema::hasColumn('vendors', 'city')) {
                $table->string('city')->after('address');
            }

            if (!Schema::hasColumn('vendors', 'state')) {
                $table->string('state')->after('city');
            }

            if (!Schema::hasColumn('vendors', 'country')) {
                $table->string('country')->after('state');
            }

            if (!Schema::hasColumn('vendors', 'postal_code')) {
                $table->string('postal_code')->after('country');
            }

            if (!Schema::hasColumn('vendors', 'description')) {
                $table->text('description')->nullable()->after('postal_code');
            }

            if (!Schema::hasColumn('vendors', 'rating')) {
                $table->decimal('rating', 3, 2)->default(0)->after('description');
            }

            if (!Schema::hasColumn('vendors', 'review_count')) {
                $table->integer('review_count')->default(0)->after('rating');
            }

            if (!Schema::hasColumn('vendors', 'logo')) {
                $table->string('logo')->nullable()->after('review_count');
            }

            if (!Schema::hasColumn('vendors', 'website')) {
                $table->string('website')->nullable()->after('logo');
            }

            if (!Schema::hasColumn('vendors', 'is_verified')) {
                $table->boolean('is_verified')->default(false)->after('review_count');
            }

            if (!Schema::hasColumn('vendors', 'is_active')) {
                $table->boolean('is_active')->default(true)->after('is_verified');
            }

            if (!Schema::hasColumn('vendors', 'deleted_at')) {
                $table->softDeletes();
            }
        });
    }

    public function down(): void
    {
        Schema::table('vendors', function (Blueprint $table) {
            $columns = [
                'slug',
                'contact_person',
                'address',
                'city',
                'state',
                'country',
                'postal_code',
                'description',
                'logo',
                'website',
                'rating',
                'review_count',
                'is_verified',
                'is_active',
                'deleted_at'
            ];

            foreach ($columns as $column) {
                if (Schema::hasColumn('vendors', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
