<?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
    {
        // إضافة user_type إلى جدول المستخدمين إذا لم يكن موجوداً
        if (!Schema::hasColumn('users', 'user_type')) {
            Schema::table('users', function (Blueprint $table) {
                $table->enum('user_type', ['driver', 'passenger', 'admin', 'restaurant_owner', 'gas_owner'])->default('passenger')->after('email');
            });
        }

        // إضافة الحقول المفقودة إلى جدول السائقين
        Schema::table('drivers', function (Blueprint $table) {
            if (!Schema::hasColumn('drivers', 'latitude')) {
                $table->decimal('latitude', 10, 8)->nullable()->after('emergency_contact');
            }
            if (!Schema::hasColumn('drivers', 'longitude')) {
                $table->decimal('longitude', 11, 8)->nullable()->after('latitude');
            }
            if (!Schema::hasColumn('drivers', 'current_address')) {
                $table->string('current_address')->nullable()->after('longitude');
            }
            if (!Schema::hasColumn('drivers', 'service_areas')) {
                $table->json('service_areas')->nullable()->after('current_address');
            }
        });

        // إضافة صاحب المطعم إلى جدول المطاعم
        Schema::table('restaurants', function (Blueprint $table) {
            if (!Schema::hasColumn('restaurants', 'owner_id')) {
                $table->foreignId('owner_id')->nullable()->constrained('users')->onDelete('set null')->after('id');
            }
            if (!Schema::hasColumn('restaurants', 'service_areas')) {
                $table->json('service_areas')->nullable()->after('delivery_time_minutes');
            }
        });

        // إنشاء جدول مكاتب الغاز
        if (!Schema::hasTable('gas_offices')) {
            Schema::create('gas_offices', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->text('description')->nullable();
                $table->string('address');
                $table->decimal('latitude', 10, 8)->nullable();
                $table->decimal('longitude', 11, 8)->nullable();
                $table->string('phone');
                $table->string('email')->nullable();
                $table->foreignId('owner_id')->nullable()->constrained('users')->onDelete('set null');
                $table->string('logo')->nullable();
                $table->string('cover_image')->nullable();
                $table->boolean('is_active')->default(true);
                $table->boolean('is_open')->default(true);
                $table->time('opening_time')->nullable();
                $table->time('closing_time')->nullable();
                $table->json('service_areas')->nullable();
                $table->timestamps();
            });
        }

        // إضافة مكتب الغاز إلى جدول قناني الغاز
        Schema::table('gas_cylinders', function (Blueprint $table) {
            if (!Schema::hasColumn('gas_cylinders', 'gas_office_id')) {
                $table->foreignId('gas_office_id')->nullable()->constrained('gas_offices')->onDelete('set null')->after('id');
            }
        });

        // إضافة مكتب الغاز إلى جدول طلبات الغاز
        Schema::table('gas_orders', function (Blueprint $table) {
            if (!Schema::hasColumn('gas_orders', 'gas_office_id')) {
                $table->foreignId('gas_office_id')->nullable()->constrained('gas_offices')->onDelete('set null')->after('id');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // إزالة الحقول المضافة
        Schema::table('drivers', function (Blueprint $table) {
            $table->dropColumn(['latitude', 'longitude', 'current_address', 'service_areas']);
        });

        Schema::table('restaurants', function (Blueprint $table) {
            $table->dropForeign(['owner_id']);
            $table->dropColumn(['owner_id', 'service_areas']);
        });

        Schema::table('gas_cylinders', function (Blueprint $table) {
            $table->dropForeign(['gas_office_id']);
            $table->dropColumn('gas_office_id');
        });

        Schema::table('gas_orders', function (Blueprint $table) {
            $table->dropForeign(['gas_office_id']);
            $table->dropColumn('gas_office_id');
        });

        Schema::dropIfExists('gas_offices');
    }
};
