<?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('numbering_rules', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID');
            $table->string('document_type', 50)->comment('문서유형: quote, order, sale, work_order, material_receipt');
            $table->string('rule_name', 100)->nullable()->comment('규칙명 (관리용)');
            $table->json('pattern')->comment('패턴 정의 (세그먼트 배열)');
            $table->string('reset_period', 20)->default('daily')->comment('시퀀스 리셋 주기: daily, monthly, yearly, never');
            $table->integer('sequence_padding')->default(2)->comment('시퀀스 자릿수');
            $table->boolean('is_active')->default(true);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            $table->unique(['tenant_id', 'document_type'], 'uq_tenant_doctype');
            $table->index('tenant_id', 'idx_numbering_rules_tenant');
        });
    }

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