<?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('conversations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('contact_phone', 30);
            $table->string('contact_name', 100)->nullable();
            $table->enum('status', ['new', 'open', 'closed'])->default('new');
            $table->string('label', 50)->nullable();
            $table->string('language', 20)->nullable();
            $table->text('last_message')->nullable();
            $table->timestamp('last_message_at')->nullable();
            $table->timestamp('customer_last_message_at')->nullable(); // for 24hr window
            $table->unsignedSmallInteger('unread_count')->default(0);
            $table->timestamps();
            $table->unique(['user_id', 'contact_phone']);
        });
    }

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