<?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('chats', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id_1'); // First user in the chat
            $table->unsignedBigInteger('user_id_2'); // Second user in the chat
            $table->unsignedBigInteger('connection_request_id')->nullable(); // Link to user_connections (either direction)
            $table->integer('questions_exchanged')->default(0); // Track number of questions exchanged
            $table->enum('status', ['active', 'pending_decision', 'completed', 'disconnected', 'need_more_time'])->default('active');
            $table->timestamp('decision_deadline')->nullable(); // For "need more time" option
            $table->boolean('user_1_decision')->nullable(); // null=no decision, true=yes, false=no
            $table->boolean('user_2_decision')->nullable(); // null=no decision, true=yes, false=no
            $table->timestamp('last_reminder_sent_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            // Foreign keys
            $table->foreign('user_id_1')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('user_id_2')->references('id')->on('users')->onDelete('cascade');

            // Indexes for performance
            $table->index(['user_id_1', 'user_id_2']);
            $table->index('status');
            $table->index('decision_deadline');
        });
    }

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