<?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('communication_events', function (Blueprint $table) {
            $table->id();
            $table->foreignId('communication_id')->constrained()->cascadeOnDelete();
            $table->foreignId('solicitor_id')->constrained()->cascadeOnDelete();

            $table->foreignId('call_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('agent_id')->nullable()->constrained('users')->nullOnDelete();

            $table->string('event', 100)->index();
            $table->string('source', 50)->default('system')->index();

            $table->text('description')->nullable();
            $table->json('payload')->nullable();

            $table->timestamp('occurred_at')->nullable()->index();

            $table->timestamps();

            $table->index(['solicitor_id', 'event']);
            $table->index(['solicitor_id', 'occurred_at']);
        });
    }

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