<?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('username_history', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('old_username', 20);
            $table->timestamp('changed_at');
            $table->timestamps();

            $table->index(['user_id', 'changed_at']);
            $table->index('old_username');
        });

        Schema::create('username_redirects', function (Blueprint $table) {
            $table->id();
            $table->string('old_username', 20)->unique();
            $table->string('new_username', 20);
            $table->foreignId('user_id')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();

            $table->index('new_username');
        });
    }

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