<?php

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

/**
 * Document-type taxonomy (spec §8). business_id NULL = system type (shared,
 * read-only in the UI); a non-null business_id = a tenant's custom type. The
 * 15 system types are seeded by MeddocTypesSeeder.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('meddoc_types')) {
            Schema::create('meddoc_types', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id')->nullable(); // null = system type
                $t->string('type_key', 100);
                $t->string('label', 191);
                $t->text('description')->nullable();
                $t->unsignedInteger('retention_days')->nullable();
                $t->boolean('requires_signature')->default(false);
                $t->boolean('is_active')->default(true);
                $t->timestamps();

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

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