<?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
    {
        if (Schema::hasTable('nx_worklist')) {
            return;
        }

        Schema::create('nx_worklist', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id')->nullable()->index();
            $table->string('sample_no', 13)->unique();
            $table->string('patient_id', 13)->nullable();
            $table->string('patient_name', 60)->nullable();
            $table->tinyInteger('species')->default(0);
            $table->tinyInteger('sex')->default(9)->comment('0=Male, 1=Female, 9=Unknown');
            $table->unsignedSmallInteger('age')->default(999);
            $table->json('test_names')->nullable()->comment('Array of test names to run, max 30');
            $table->boolean('is_sent')->default(false)->comment('Whether the device has retrieved this entry');
            $table->timestamp('sent_at')->nullable();
            // VetClinic sync linkage
            $table->unsignedInteger('contact_id')->nullable()->index()->comment('VetClinic pet owner contact');
            $table->unsignedInteger('vc_pet_id')->nullable()->index()->comment('VetClinic pet');
            $table->unsignedBigInteger('created_by')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('sample_no');
            $table->index('is_sent');
        });
    }

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