<?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('voicemails', function (Blueprint $table) {
            $table->id();

            $table->foreignId('communication_id')->constrained()->cascadeOnDelete();
            $table->foreignId('call_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('solicitor_id')->constrained()->cascadeOnDelete();

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

            $table->string('provider', 50)->default('twilio');
            $table->string('provider_recording_sid')->nullable()->index();

            $table->string('from_number', 50)->nullable()->index();
            $table->string('to_number', 50)->nullable()->index();

            $table->text('recording_url')->nullable();
            $table->unsignedInteger('duration')->nullable();

            $table->boolean('is_listened')->default(false)->index();
            $table->boolean('callback_requested')->default(false)->index();

            $table->text('notes')->nullable();
            $table->timestamp('received_at')->nullable()->index();

            $table->json('meta')->nullable();

            $table->timestamps();

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

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