<?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('push_subscriptions', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->nullable()->constrained()->cascadeOnDelete();
            $t->foreignId('user_id')->nullable()->constrained()->cascadeOnDelete();
            $t->string('endpoint', 500);
            $t->string('public_key')->nullable();      // p256dh
            $t->string('auth_token')->nullable();       // auth
            $t->string('content_encoding', 32)->default('aesgcm');
            $t->string('user_agent')->nullable();
            $t->timestamps();

            $t->unique('endpoint', 'push_subscriptions_endpoint_unique');
            $t->index(['company_id', 'user_id']);
        });
    }

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