<?php

use App\Domains\Auth\Models\ResetPasswordAttempt;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

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

        // Attempts created before this migration did not have an expiration instant.
        DB::table('reset_password_attempts')
            ->whereIn('status', [
                ResetPasswordAttempt::STATUS_PENDING,
                ResetPasswordAttempt::STATUS_VALIDATED,
            ])
            ->update(['status' => ResetPasswordAttempt::STATUS_EXPIRED]);
    }

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