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

            // «کانال چت» همان coach_request است — دلیل در docs/architecture-addendum.md
            $table->foreignId('coach_request_id')->constrained('coach_requests')->cascadeOnDelete();
            $table->foreignId('sender_id')->constrained('users')->cascadeOnDelete();

            $table->text('body');
            $table->timestamp('read_at')->nullable();

            $table->timestamps();
            $table->softDeletes(); // حذف پیام توسط فرستنده — Soft Delete تا سابقه در Audit از بین نرود

            $table->index(['coach_request_id', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('messages');
    }
};
