<?php

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

/**
 * lab_worklist — pending orders sent to an analyzer (bidirectional protocols).
 * May be raised manually or against a VetClinic pet.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('lab_worklist')) {
            return;
        }

        Schema::create('lab_worklist', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id')->nullable()->index();
            $table->unsignedInteger('agent_id')->nullable()->index();
            $table->unsignedInteger('device_id')->nullable()->index();
            $table->string('vendor', 30)->index();
            $table->string('sample_no', 30)->index();
            $table->string('patient_id', 30)->nullable();
            $table->string('patient_name', 80)->nullable();
            $table->string('species', 40)->nullable();
            $table->string('species_code', 10)->nullable()->comment('Vendor numeric/text species code');
            $table->string('sex', 10)->nullable();
            $table->unsignedSmallInteger('age')->nullable();
            $table->json('tests')->nullable()->comment('Analyte codes / panels to run');
            $table->enum('status', ['pending', 'sent', 'completed', 'cancelled'])->default('pending');
            $table->timestamp('sent_at')->nullable();
            // VetClinic sync linkage
            $table->unsignedInteger('contact_id')->nullable()->index();
            $table->unsignedInteger('vc_pet_id')->nullable()->index();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();
            $table->softDeletes();

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

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