<?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::table('findings', function (Blueprint $table): void {
            $table->timestampTz('first_seen_at')->nullable();
            $table->timestampTz('last_seen_at')->nullable();
            $table->integer('times_seen')->nullable()->default(0);

            $table->smallInteger('sla_days')->nullable();
            $table->timestampTz('due_at')->nullable();

            $table->foreignId('owner_user_id')->nullable()->constrained('users');
            $table->foreignId('assignee_user_id')->nullable()->constrained('users');

            $table->timestampTz('triaged_at')->nullable();
            $table->timestampTz('in_progress_at')->nullable();
            $table->timestampTz('reopened_at')->nullable();

            $table->timestampTz('closed_at')->nullable();
            $table->foreignId('closed_by_user_id')->nullable()->constrained('users');
            $table->string('closed_reason', 255)->nullable();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('findings', function (Blueprint $table): void {
            $table->dropConstrainedForeignId('closed_by_user_id');
            $table->dropConstrainedForeignId('assignee_user_id');
            $table->dropConstrainedForeignId('owner_user_id');

            $table->dropColumn([
                'first_seen_at',
                'last_seen_at',
                'times_seen',
                'sla_days',
                'due_at',
                'triaged_at',
                'in_progress_at',
                'reopened_at',
                'closed_at',
                'closed_reason',
            ]);
        });
    }
};
