<?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('prescriptions', 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->string('prescription_number', 50)->nullable();
            $table->text('diagnosis_summary')->nullable();
            $table->text('notes')->nullable();

            $table->enum('status', ['draft', 'signed', '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', 'status']);
            $table->unique(['company_id', 'prescription_number']);
        });
    }

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