<?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
    {
        Schema::create('clinical_orders', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('clinical_history_id')->constrained()->cascadeOnDelete();
            $table->foreignId('clinical_evolution_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('customer_id')->constrained()->cascadeOnDelete();
            $table->foreignId('employee_id')->nullable()->constrained()->nullOnDelete();

            $table->enum('type', [
                'laboratory',
                'imaging',
                'procedure',
                'referral',
                'care_recommendation',
                'aesthetic_plan',
                'therapy_plan',
                'other',
            ]);
            $table->string('title');
            $table->text('description')->nullable();
            $table->text('notes')->nullable();

            $table->enum('status', ['draft', 'signed', 'fulfilled', 'cancelled'])->default('draft');
            $table->timestamp('signed_at')->nullable();
            $table->foreignId('signed_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();

            $table->timestamps();
            $table->softDeletes();

            $table->index(['company_id', 'customer_id', 'type']);
        });
    }

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