<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * Secure share links with expiry + revocation (spec §8). token is a 64-char
 * random string, the only credential for the public /med-docs/shared/{token}
 * route. access_count is bumped on each public open for a light audit summary.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('meddoc_shares')) {
            Schema::create('meddoc_shares', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('document_id');
                $t->string('token', 64)->unique();
                $t->timestamp('expires_at')->nullable();
                $t->unsignedInteger('created_by')->nullable();
                $t->timestamp('revoked_at')->nullable();
                $t->unsignedInteger('access_count')->default(0);
                $t->timestamps();

                $t->index(['business_id', 'document_id']);
            });
        }
    }

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