<?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('tickets', function (Blueprint $table) {
            $table->id();
            $table->string('ticket_number')->unique()->nullable();
            $table->string('title');
            $table->text('description');
            $table->foreignId('project_id')->nullable()->constrained('projects');
            $table->foreignId('requester_id')->constrained('users');
            $table->foreignId('assigned_to')->nullable()->constrained('users');
            $table->enum('status', ['abierto', 'en_progreso', 'resuelto', 'cerrado'])
                  ->default('abierto');
            $table->enum('priority', ['baja', 'media', 'alta', 'critica'])
                  ->default('media');
            $table->datetime('due_date')->nullable();
            $table->datetime('resolved_at')->nullable();
            $table->datetime('closed_at')->nullable();
            $table->text('resolution_notes')->nullable();
            $table->timestamps();

            // Indexes for better performance
            $table->index(['status', 'priority']);
            $table->index(['project_id', 'status']);
            $table->index(['assigned_to', 'status']);
            $table->index('created_at');
        });
    }

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