<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Condiciones específicas del cliente (alergias, contraindicaciones, etc.)
     * normalizadas como filas separadas para poder etiquetar y alertar.
     */
    public function up(): void
    {
        Schema::create('client_conditions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('clinical_history_id')->constrained()->cascadeOnDelete();

            $table->enum('type', [
                'allergy', 'disease', 'medication',
                'contraindication', 'sensitivity', 'other',
            ]);
            $table->string('name');
            $table->text('description')->nullable();
            $table->enum('severity', ['low', 'medium', 'high', 'critical'])->default('low');
            $table->boolean('is_active')->default(true);
            $table->date('detected_at')->nullable();

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

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

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