<?php

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

/**
 * Per-business lab provider config. One row per (business, key); the active
 * row drives where lab orders are routed. Encrypted credentials hold API
 * tokens / HL7 endpoints for cloud reference labs.
 */
class CreateNxLabProvidersTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('nx_lab_providers')) return;

        Schema::create('nx_lab_providers', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id')->index();
            $table->string('key', 30);                       // nx600 | idexx | antech | ...
            $table->string('display_name', 100);
            $table->text('endpoint')->nullable();
            $table->text('credentials')->nullable();         // Crypt::encrypt(JSON)
            $table->boolean('enabled')->default(true);
            $table->boolean('is_default')->default(false);
            $table->timestamps();

            $table->unique(['business_id', 'key']);
        });
    }

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