<?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_patients')) {
            return;
        }

        Schema::create('nx_patients', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id')->nullable()->index();
            $table->string('patient_id', 13)->nullable()->comment('External / device patient code');
            $table->string('patient_name', 60)->nullable();
            $table->tinyInteger('sex')->default(9)->comment('0=Male, 1=Female, 9=Unknown');
            $table->unsignedSmallInteger('age')->default(999)->comment('999=Unknown');
            $table->tinyInteger('species')->default(0)->comment('0-99 NX600 species code');
            // VetClinic sync linkage (nullable in standalone mode)
            $table->unsignedInteger('contact_id')->nullable()->index()->comment('VetClinic pet owner contact');
            $table->unsignedInteger('vc_pet_id')->nullable()->index()->comment('VetClinic pet');
            $table->timestamps();

            $table->index('patient_id');
        });
    }

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