<?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('delivery_men', function (Blueprint $table) {
            if (!Schema::hasColumn('delivery_men', 'verify')) {
                $table->string('verify', 10)->nullable()->after('password');
            }
            if (!Schema::hasColumn('delivery_men', 'password_set')) {
                $table->boolean('password_set')->default(false)->after('verify');
            }
            if (!Schema::hasColumn('delivery_men', 'approval_status')) {
                $table->string('approval_status', 32)->default('pending')->after('status');
            }
            if (!Schema::hasColumn('delivery_men', 'nid_front')) {
                $table->string('nid_front')->nullable()->after('photo');
            }
            if (!Schema::hasColumn('delivery_men', 'nid_back')) {
                $table->string('nid_back')->nullable()->after('nid_front');
            }
            if (!Schema::hasColumn('delivery_men', 'zone_ids')) {
                $table->json('zone_ids')->nullable()->after('nid_back');
            }
            if (!Schema::hasColumn('delivery_men', 'accepts_requests')) {
                $table->boolean('accepts_requests')->default(true)->after('is_online');
            }
            if (!Schema::hasColumn('delivery_men', 'platform_commission_percent')) {
                $table->decimal('platform_commission_percent', 5, 2)->default(0)->after('accepts_requests');
            }
            if (!Schema::hasColumn('delivery_men', 'current_lat')) {
                $table->decimal('current_lat', 10, 7)->nullable();
            }
            if (!Schema::hasColumn('delivery_men', 'current_lng')) {
                $table->decimal('current_lng', 10, 7)->nullable();
            }
        });

        if (!Schema::hasTable('warehouses')) {
            Schema::create('warehouses', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->text('address')->nullable();
                $table->decimal('lat', 10, 7);
                $table->decimal('lng', 10, 7);
                $table->unsignedBigInteger('local_delivery_zone_id')->nullable();
                $table->boolean('is_enabled')->default(true);
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('delivery_order_offers')) {
            Schema::create('delivery_order_offers', function (Blueprint $table) {
                $table->id();
                $table->unsignedBigInteger('order_id');
                $table->unsignedBigInteger('delivery_man_id');
                $table->string('status', 20)->default('pending');
                $table->timestamp('expires_at')->nullable();
                $table->timestamps();
                $table->unique(['order_id', 'delivery_man_id']);
                $table->index('order_id');
                $table->index('delivery_man_id');
            });
        }

        Schema::table('shippings', function (Blueprint $table) {
            if (!Schema::hasColumn('shippings', 'local_delivery_zone_id')) {
                $table->unsignedBigInteger('local_delivery_zone_id')->nullable()->after('delivery_man_id');
            }
            if (!Schema::hasColumn('shippings', 'warehouse_id')) {
                $table->unsignedBigInteger('warehouse_id')->nullable()->after('local_delivery_zone_id');
            }
        });

        Schema::table('general_settings', function (Blueprint $table) {
            if (!Schema::hasColumn('general_settings', 'default_rider_commission_percent')) {
                $table->decimal('default_rider_commission_percent', 5, 2)->default(10)->after('local_delivery_enabled');
            }
        });
    }

    public function down(): void
    {
        Schema::table('general_settings', function (Blueprint $table) {
            if (Schema::hasColumn('general_settings', 'default_rider_commission_percent')) {
                $table->dropColumn('default_rider_commission_percent');
            }
        });

        Schema::table('shippings', function (Blueprint $table) {
            if (Schema::hasColumn('shippings', 'warehouse_id')) {
                $table->dropColumn('warehouse_id');
            }
            if (Schema::hasColumn('shippings', 'local_delivery_zone_id')) {
                $table->dropColumn('local_delivery_zone_id');
            }
        });

        Schema::dropIfExists('delivery_order_offers');
        Schema::dropIfExists('warehouses');

        Schema::table('delivery_men', function (Blueprint $table) {
            $cols = ['verify', 'password_set', 'approval_status', 'nid_front', 'nid_back', 'zone_ids', 'accepts_requests', 'platform_commission_percent', 'current_lat', 'current_lng'];
            foreach ($cols as $col) {
                if (Schema::hasColumn('delivery_men', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
};
