<?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_diagnoses', function (Blueprint $table) {
            $table->id();
            $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->string('cie10_code', 20)->nullable();
            $table->string('name');
            $table->enum('type', ['primary', 'secondary', 'impression'])->default('primary');
            $table->text('notes')->nullable();

            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();

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

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

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