<?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('portal_otp_challenges', function (Blueprint $t) {
            // UUID = the challenge id echoed back by the PWA on /auth/otp/verify.
            $t->uuid('id')->primary();
            $t->unsignedBigInteger('owner_id')->nullable();
            $t->foreign('owner_id')->references('id')->on('portal_owners')->onDelete('set null');
            $t->string('phone', 32)->index();
            // Bcrypt of the 6-digit code.
            $t->string('code_hash');
            $t->unsignedTinyInteger('attempts')->default(0);
            $t->timestamp('expires_at')->index();
            $t->timestamp('used_at')->nullable();
            $t->timestamps();
        });
    }

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