<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('local_delivery_zones', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->double('lat');
            $table->double('lng');
            $table->double('radius'); // Radius in kilometers
            $table->decimal('delivery_charge', 10, 2)->default(0.00);
            $table->boolean('is_enabled')->default(true);
            $table->timestamps();
        });

        Schema::create('local_delivery_points', function (Blueprint $table) {
            $table->id();
            $table->foreignId('zone_id')->constrained('local_delivery_zones')->onDelete('cascade');
            $table->string('name');
            $table->double('lat')->nullable();
            $table->double('lng')->nullable();
            $table->decimal('extra_charge', 10, 2)->default(0.00);
            $table->timestamps();
        });

        Schema::table('general_settings', function (Blueprint $table) {
            if (!Schema::hasColumn('general_settings', 'local_delivery_enabled')) {
                $table->boolean('local_delivery_enabled')->default(false)->after('id');
            }
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('general_settings', function (Blueprint $table) {
            if (Schema::hasColumn('general_settings', 'local_delivery_enabled')) {
                $table->dropColumn('local_delivery_enabled');
            }
        });

        Schema::dropIfExists('local_delivery_points');
        Schema::dropIfExists('local_delivery_zones');
    }
};
