<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * PRC-1143 — konsolide şema: `chat_sessions` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('chat_sessions', function (Blueprint $t) {
            $t->id();
            $t->unsignedBigInteger('client_id')->nullable();
            $t->unsignedBigInteger('admin_id')->nullable();
            $t->unsignedBigInteger('department_id')->nullable();
            $t->string('visitor_name', 255)->nullable();
            $t->string('visitor_email', 255)->nullable();
            $t->string('public_token', 255)->nullable();
            $t->string('visitor_id', 255)->nullable();
            $t->string('ip_address', 255)->nullable();
            $t->string('country', 255)->nullable();
            $t->text('user_agent')->nullable();
            $t->string('start_url', 255)->nullable();
            $t->string('referrer', 255)->nullable();
            $t->string('status', 255)->default('waiting');
            $t->unsignedTinyInteger('rating')->nullable();
            $t->text('rating_comment')->nullable();
            $t->timestamp('started_at')->nullable();
            $t->timestamp('ended_at')->nullable();
            $t->string('closed_by', 255)->nullable();
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('admin_id', 'chat_sessions_admin_id_foreign');
            $t->index('client_id', 'chat_sessions_client_id_foreign');
            $t->unique('public_token', 'chat_sessions_public_token_unique');
            $t->index(['status', 'created_at'], 'chat_sessions_status_created_at_index');
            $t->foreign('admin_id', 'chat_sessions_admin_id_foreign')->references('id')->on('admins')->onDelete('set null');
            $t->foreign('client_id', 'chat_sessions_client_id_foreign')->references('id')->on('clients')->onDelete('set null');
        });
    }

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