<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('contact_template_triggers', function (Blueprint $t) {
            $t->id();
            $t->foreignId('contact_template_id')->constrained()->cascadeOnDelete();
            $t->foreignId('solicitor_id')->constrained()->cascadeOnDelete();
            $t->string('trigger_type', 32);
            $t->string('model_type')->nullable();
            $t->json('conditions')->nullable();
            $t->unsignedInteger('dedupe_window_minutes')->default(60);
            $t->boolean('is_active')->default(true);
            $t->timestamps();

            $t->index(
                ['solicitor_id', 'trigger_type', 'model_type', 'is_active'],
                'contact_template_triggers_lookup'
            );
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('contact_template_triggers');
    }
};
