<?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('communication_logs', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->onDelete('cascade');
            $table->foreignId('customer_id')->nullable()->constrained('users')->onDelete('set null');
            $table->string('customer_name');
            $table->enum('channel', ['call', 'sms', 'email', 'whatsapp', 'in_app_chat'])->default('in_app_chat');
            $table->enum('direction', ['incoming', 'outgoing'])->default('incoming');
            $table->integer('duration')->nullable(); // in seconds for calls
            $table->longText('message_preview');
            $table->timestamp('timestamp')->nullable();
            $table->string('agent')->nullable();
            $table->enum('sentiment', ['positive', 'neutral', 'negative'])->default('neutral');
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();

            // Indexes
            $table->index('tenant_id');
            $table->index('customer_id');
            $table->index('channel');
            $table->index('direction');
            $table->index('sentiment');
            $table->index('timestamp');
            $table->index('created_at');
        });
    }

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