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

            $table->string('extension', 20)->nullable();
            $table->string('status', 50)->default('offline')->index();

            $table->foreignId('current_communication_id')->nullable()->constrained('communications')->nullOnDelete();

            $table->timestamp('status_changed_at')->nullable()->index();
            $table->timestamp('last_seen_at')->nullable()->index();

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

            $table->timestamps();

            $table->unique(['solicitor_id', 'agent_id']);
            $table->unique(['solicitor_id', 'extension']);
            $table->index(['solicitor_id', 'status']);
        });
    }

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