<?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::create('targeting_locations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('targeting_set_id')->constrained()->cascadeOnDelete();
            $table->foreignId('location_id')->nullable()->constrained('locations');
            $table->foreignId('platform_location_id')->nullable()->constrained('platform_locations');
            $table->string('operation', 16)->default('include');
            $table->string('kind', 24);
            $table->string('display_label');
            $table->char('country_code', 2)->nullable();
            $table->decimal('latitude', 10, 7)->nullable();
            $table->decimal('longitude', 10, 7)->nullable();
            $table->decimal('radius', 8, 2)->nullable();
            $table->char('radius_unit', 2)->nullable();
            $table->timestamps();

            $table->unique(['targeting_set_id', 'location_id', 'operation'], 'targeting_loc_set_location_op_uniq');
            $table->index(['targeting_set_id', 'kind', 'operation'], 'targeting_loc_set_kind_op_idx');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('targeting_locations');
    }
};
