<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Modules\Auth\Entities\User;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('public_profile_token', 64)->nullable()->unique()->after('member_type');
        });

        User::withTrashed()
            ->whereNull('public_profile_token')
            ->cursor()
            ->each(function (User $user) {
                $user->forceFill([
                    'public_profile_token' => Str::random(48),
                ])->saveQuietly();
            });
    }

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