<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::dropIfExists('delivery_zone_locations');
        Schema::dropIfExists('delivery_zones');

        if (Schema::hasTable('allocation_rules')) {
            DB::table('allocation_rules')
                ->where('rule_type', 'delivery_zone')
                ->delete();
        }

        $permissionIds = DB::table('permissions')
            ->where('name', 'delivery-zones.manage')
            ->pluck('id');

        if ($permissionIds->isNotEmpty()) {
            DB::table('role_has_permissions')
                ->whereIn('permission_id', $permissionIds)
                ->delete();

            if (Schema::hasTable('model_has_permissions')) {
                DB::table('model_has_permissions')
                    ->whereIn('permission_id', $permissionIds)
                    ->delete();
            }

            DB::table('permissions')
                ->whereIn('id', $permissionIds)
                ->delete();
        }
    }

    public function down(): void
    {
        if (! Schema::hasTable('delivery_zones')) {
            Schema::create('delivery_zones', function (Blueprint $table) {
                $table->id();
                $table->foreignId('company_id')->constrained('companies')->cascadeOnDelete();
                $table->string('name');
                $table->string('pincode_from', 20)->nullable();
                $table->string('pincode_to', 20)->nullable();
                $table->string('pincode', 20)->nullable();
                $table->boolean('is_active')->default(true);
                $table->timestamps();

                $table->index(['company_id', 'pincode', 'is_active']);
            });
        }

        if (! Schema::hasTable('delivery_zone_locations')) {
            Schema::create('delivery_zone_locations', function (Blueprint $table) {
                $table->id();
                $table->foreignId('delivery_zone_id')->constrained('delivery_zones')->cascadeOnDelete();
                $table->foreignId('location_id')->constrained('locations')->cascadeOnDelete();
                $table->unsignedInteger('priority')->default(100);
                $table->timestamps();

                $table->unique(['delivery_zone_id', 'location_id'], 'dz_location_unique');
            });
        }

        $exists = DB::table('permissions')
            ->where('name', 'delivery-zones.manage')
            ->where('guard_name', 'web')
            ->exists();

        if (! $exists) {
            DB::table('permissions')->insert([
                'name' => 'delivery-zones.manage',
                'guard_name' => 'web',
                'created_at' => now(),
                'updated_at' => now(),
            ]);
        }
    }
};
