<?php

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

/**
 * Reference-number counters (§80). One row per (type, scope-key) — for example
 * invoice numbers per branch per year — locked on read so concurrent requests
 * cannot mint the same number.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('number_sequences', function (Blueprint $table) {
            $table->id();
            $table->string('type', 40);
            $table->string('scope_key', 64)->default('-');
            $table->unsignedBigInteger('next_value')->default(1);
            $table->timestamps();

            $table->unique(['type', 'scope_key']);
        });
    }

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