<?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('issues', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->nullable()->unique();

            $table->foreignId('project_id')
                ->constrained('projects')
                ->cascadeOnDelete();

            $table->foreignId('feature_id')
                ->nullable()
                ->constrained('features')
                ->nullOnDelete();

            $table->foreignId('inspection_id')
                ->nullable()
                ->constrained('inspections')
                ->nullOnDelete();

            $table->string('title');
            $table->text('description')->nullable();

            $table->enum('status', ['open', 'in_review', 'resolved', 'closed'])
                ->default('open');

            $table->enum('priority', ['low', 'medium', 'high', 'critical'])
                ->default('medium');

            $table->string('type', 30)->default('other');

            $table->foreignId('reported_by')
                ->constrained('users');

            $table->foreignId('assigned_to')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            $table->timestamp('resolved_at')->nullable();
            $table->text('resolution_notes')->nullable();
            $table->timestamp('client_updated_at')->nullable();

            $table->timestamps();
            $table->softDeletes();
        });
    }

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