<?php

declare(strict_types=1);

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::table('support_tickets', function (Blueprint $table): void {
            $table->foreignId('department_id')->nullable()->after('department')->constrained('departments')->nullOnDelete();
            $table->foreignId('assigned_to')->nullable()->after('department_id')->constrained('users')->nullOnDelete();
            $table->foreignId('sla_policy_id')->nullable()->after('assigned_to')->constrained('sla_policies')->nullOnDelete();
            $table->timestamp('first_response_due_at')->nullable()->after('last_reply_at');
            $table->timestamp('resolution_due_at')->nullable()->after('first_response_due_at');
            $table->timestamp('first_responded_at')->nullable()->after('resolution_due_at');
            $table->timestamp('resolved_at')->nullable()->after('first_responded_at');
            $table->boolean('sla_first_response_breached')->default(false)->after('resolved_at');
            $table->boolean('sla_resolution_breached')->default(false)->after('sla_first_response_breached');
            $table->foreignId('merged_into_ticket_id')->nullable()->after('sla_resolution_breached')->constrained('support_tickets')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('support_tickets', function (Blueprint $table): void {
            $table->dropForeign(['department_id']);
            $table->dropForeign(['assigned_to']);
            $table->dropForeign(['sla_policy_id']);
            $table->dropForeign(['merged_into_ticket_id']);
            $table->dropColumn([
                'department_id',
                'assigned_to',
                'sla_policy_id',
                'first_response_due_at',
                'resolution_due_at',
                'first_responded_at',
                'resolved_at',
                'sla_first_response_breached',
                'sla_resolution_breached',
                'merged_into_ticket_id',
            ]);
        });
    }
};
