<?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('recordings', 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->string('provider', 50)->default('twilio');
            $table->string('provider_recording_sid')->nullable()->unique();
            $table->string('provider_call_sid')->nullable()->index();

            $table->string('status', 50)->nullable()->index();
            $table->unsignedInteger('duration')->nullable();

            $table->text('recording_url')->nullable();
            $table->string('storage_disk')->nullable();
            $table->string('storage_path')->nullable();

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

            $table->timestamps();

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

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