<?php

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

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

        $expiryDays = (int) config('user.public_profile_token_ttl_days', 90);

        User::withTrashed()
            ->whereNotNull('public_profile_token')
            ->whereNull('public_profile_token_expires_at')
            ->cursor()
            ->each(function (User $user) use ($expiryDays) {
                $user->forceFill([
                    'public_profile_token_expires_at' => now()->addDays($expiryDays),
                ])->saveQuietly();
            });
    }

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