<?php

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

/**
 * lab_analyte_map — maps a vendor's analyte code to a canonical name, unit and
 * species-specific reference range. Seeded per vendor module; per-business
 * overrides allowed via business_id.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('lab_analyte_map')) {
            return;
        }

        Schema::create('lab_analyte_map', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id')->nullable()->index()->comment('Null = global default');
            $table->string('vendor', 30)->index();
            $table->string('model', 60)->nullable();
            $table->string('analyte_code', 20)->index();
            $table->string('canonical_name', 60);
            $table->string('display_name', 60)->nullable();
            $table->string('unit', 16)->nullable();
            $table->string('species', 40)->nullable()->comment('Reference ranges are species-specific');
            $table->string('ref_lower', 16)->nullable();
            $table->string('ref_upper', 16)->nullable();
            $table->string('category', 40)->nullable()->comment('chemistry, hematology, electrolytes...');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();

            $table->index(['vendor', 'analyte_code']);
        });
    }

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