<?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_message_events', function (Blueprint $t) {
            $t->id();
            $t->foreignId('contact_message_id')->constrained()->cascadeOnDelete();
            $t->foreignId('solicitor_id')->constrained()->cascadeOnDelete();
            $t->string('event_type', 32);
            $t->string('provider_event_id', 191)->nullable();
            $t->string('mapped_status', 24)->nullable();
            $t->json('payload')->nullable();
            $t->timestamp('occurred_at')->index();
            $t->timestamps();

            $t->index(['contact_message_id', 'event_type']);
            $t->unique(
                ['contact_message_id', 'provider_event_id'],
                'contact_message_events_provider_unique'
            );
        });
    }

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