<?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
    {
        Schema::table('mailings', function (Blueprint $table) {
            $table->boolean('filter_by_location')->default(false)->after('type');
            $table->unsignedBigInteger('location_country_id')->nullable()->after('filter_by_location');
            $table->unsignedBigInteger('location_division_id')->nullable()->after('location_country_id');
            $table->unsignedBigInteger('location_city_id')->nullable()->after('location_division_id');
            $table->unsignedBigInteger('location_district_id')->nullable()->after('location_city_id');

            // Add foreign key constraints
            $table->foreign('location_country_id')->references('id')->on('countries')->onDelete('set null');
            $table->foreign('location_division_id')->references('id')->on('divisions')->onDelete('set null');
            $table->foreign('location_city_id')->references('id')->on('cities')->onDelete('set null');
            $table->foreign('location_district_id')->references('id')->on('districts')->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('mailings', function (Blueprint $table) {
            $table->dropForeign(['location_country_id']);
            $table->dropForeign(['location_division_id']);
            $table->dropForeign(['location_city_id']);
            $table->dropForeign(['location_district_id']);

            $table->dropColumn([
                'filter_by_location',
                'location_country_id',
                'location_division_id',
                'location_city_id',
                'location_district_id'
            ]);
        });
    }
};
