<?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('tickets', function (Blueprint $table) {
            $table->string('id')->primary();
            $table->string('title');
            $table->text('description');
            $table->string('category');
            $table->string('priority');
            $table->string('status');
            $table->string('requester')->nullable();
            $table->unsignedBigInteger('requester_id')->nullable();
            $table->string('requester_name')->nullable();
            $table->string('department')->nullable();
            $table->foreignId('assigned_to_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('sla_due')->nullable();
            $table->timestamp('first_response_sla')->nullable();
            $table->timestamp('resolution_sla')->nullable();
            $table->timestamp('first_response_at')->nullable();
            $table->boolean('first_response_met')->default(false);
            $table->timestamps();
            
            // Add indexes for better query performance
            $table->index('status');
            $table->index('priority');
            $table->index('category');
            $table->index('requester_id');
            $table->index('assigned_to_id');
            $table->index('created_at');
            $table->index('updated_at');
        });
    }

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