<?php

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

/**
 * PRC-1143 — konsolide şema: `escalation_rules` tablosunun TAM tanımı (tek dosya).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('escalation_rules', function (Blueprint $t) {
            $t->id();
            $t->string('name', 255);
            $t->unsignedBigInteger('condition_department_id')->nullable();
            $t->unsignedBigInteger('condition_status_id')->nullable();
            $t->enum('condition_priority', ['low','medium','high'])->nullable();
            $t->integer('condition_time_elapsed')->nullable();
            $t->unsignedBigInteger('action_department_id')->nullable();
            $t->unsignedBigInteger('action_status_id')->nullable();
            $t->enum('action_priority', ['low','medium','high'])->nullable();
            $t->unsignedBigInteger('action_flag_to_admin_id')->nullable();
            $t->unsignedBigInteger('action_assign_to_admin_id')->nullable();
            $t->boolean('notify_department_admins')->default(0);
            $t->json('notify_specific_admins')->nullable();
            $t->text('auto_reply_message')->nullable();
            $t->integer('sort_order')->default(0);
            $t->boolean('status')->default(1);
            $t->timestamp('created_at')->nullable();
            $t->timestamp('updated_at')->nullable();
            $t->index('action_assign_to_admin_id', 'escalation_rules_action_assign_to_admin_id_foreign');
            $t->index('action_department_id', 'escalation_rules_action_department_id_foreign');
            $t->index('action_flag_to_admin_id', 'escalation_rules_action_flag_to_admin_id_foreign');
            $t->index('action_status_id', 'escalation_rules_action_status_id_foreign');
            $t->index('condition_department_id', 'escalation_rules_condition_department_id_index');
            $t->index('condition_priority', 'escalation_rules_condition_priority_index');
            $t->index('condition_status_id', 'escalation_rules_condition_status_id_index');
            $t->index(['status', 'sort_order'], 'escalation_rules_status_sort_order_index');
            $t->foreign('action_assign_to_admin_id', 'escalation_rules_action_assign_to_admin_id_foreign')->references('id')->on('admins')->onDelete('set null');
            $t->foreign('action_department_id', 'escalation_rules_action_department_id_foreign')->references('id')->on('ticket_departments')->onDelete('set null');
            $t->foreign('action_flag_to_admin_id', 'escalation_rules_action_flag_to_admin_id_foreign')->references('id')->on('admins')->onDelete('set null');
            $t->foreign('action_status_id', 'escalation_rules_action_status_id_foreign')->references('id')->on('ticket_statuses')->onDelete('set null');
            $t->foreign('condition_department_id', 'escalation_rules_condition_department_id_foreign')->references('id')->on('ticket_departments')->onDelete('cascade');
            $t->foreign('condition_status_id', 'escalation_rules_condition_status_id_foreign')->references('id')->on('ticket_statuses')->onDelete('cascade');
        });
    }

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