<?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::table('conversations', function (Blueprint $table) {
            // Link conversation to a contact (nullable — existing rows keep working)
            $table->foreignId('contact_id')
                  ->nullable()
                  ->after('user_id')
                  ->constrained('contacts')
                  ->nullOnDelete();

            // Future agent assignment — nullable, no FK constraint yet (agents table doesn't exist)
            // When agents are built (users with role='agent'), add FK then
            $table->unsignedBigInteger('assigned_to')->nullable()->after('contact_id');
            $table->index('assigned_to');
        });
    }

    public function down(): void
    {
        Schema::table('conversations', function (Blueprint $table) {
            $table->dropIndex(['assigned_to']);
            $table->dropForeign(['contact_id']);
            $table->dropColumn(['contact_id', 'assigned_to']);
        });
    }
};
