<?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_owner_tokens', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedBigInteger('owner_id');
            $t->foreign('owner_id')->references('id')->on('portal_owners')->onDelete('cascade');
            // sha256 of the bearer token sent to the PWA — never store plaintext.
            $t->string('token_hash', 64)->unique();
            $t->string('user_agent')->nullable();
            $t->string('ip', 45)->nullable();
            $t->timestamp('last_used_at')->nullable();
            // Null = doesn't expire (rare; usually set for ~90 days).
            $t->timestamp('expires_at')->nullable()->index();
            $t->timestamps();
        });
    }

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