<?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('users', function (Blueprint $table) {
            $table->string('api_token', 20)->nullable()->unique()->after('access_token');
        });

        // Generate tokens for existing users
        \App\Models\User::whereNull('api_token')->each(function ($user) {
            $user->update(['api_token' => \Illuminate\Support\Str::upper(\Illuminate\Support\Str::random(20))]);
        });
    }

    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('api_token');
        });
    }
};
