<?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_no', 50)->unique();
            $table->unsignedBigInteger('ticket_type_id');
            $table->unsignedBigInteger('tenant_id')->default(1);
            $table->unsignedBigInteger('customer_id')->nullable();
            $table->string('title');
            $table->text('description')->nullable();
            $table->enum('priority', [
                'low',
                'medium',
                'high',
                'critical'
            ])->default('medium');

            $table->enum('status', [
                'draft',
                'open',
                'approved',
                'assigned',
                'progress',
                'pending',
                'resolved',
                'closed',
                'cancelled',
                'rejected'
            ])->default('draft');

            $table->integer('sla_minutes')->nullable();

            $table->unsignedBigInteger('created_by');

            $table->unsignedBigInteger('approved_by')->nullable();
            $table->timestamp('approved_at')->nullable();

            $table->unsignedBigInteger('rejected_by')->nullable();
            $table->timestamp('rejected_at')->nullable();
            $table->text('rejection_reason')->nullable();

            $table->timestamp('assigned_at')->nullable();
            $table->timestamp('resolved_at')->nullable();
            $table->timestamp('closed_at')->nullable();

            $table->timestamps();

            $table->index('ticket_no');
            $table->index('ticket_type_id');
            $table->index('tenant_id');
            $table->index('customer_id');
            $table->index('status');
        });
    }

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