<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Authorship tracking for the project-details modal: who created the project,
 * and who last edited it (+ when). Nullable + nullOnDelete — existing projects
 * predate this (creator unknown until... they can't be backfilled), and a
 * deleted user shouldn't cascade-delete the project (just null the reference).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->foreignId('created_by')->nullable()->after('tenant_id')
                ->constrained('users')->nullOnDelete();
            $table->foreignId('last_edited_by')->nullable()->after('created_by')
                ->constrained('users')->nullOnDelete();
            $table->timestamp('last_edited_at')->nullable()->after('last_edited_by');
        });
    }

    public function down(): void
    {
        Schema::table('projects', function (Blueprint $table) {
            $table->dropConstrainedForeignId('created_by');
            $table->dropConstrainedForeignId('last_edited_by');
            $table->dropColumn('last_edited_at');
        });
    }
};
