<?php

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

/**
 * P5-T3 dermatology deep-dive — chronic-disease severity scores as data
 * (PASI / EASI / DLQI / IGA) on a shared ClinicCore patient (1:many), so the
 * fragment can render a trend line (or table fallback) over time.
 *
 * Additive + guarded: no-ops when ClinicCore isn't installed.
 */
class CreateDermaScoresTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('derma_scores')) return;
        if (! Schema::hasTable('clc_patients')) return; // requires ClinicCore

        Schema::create('derma_scores', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('patient_id');                 // clc_patients.id
            $table->unsignedInteger('appointment_id')->nullable(); // clc_appointments.id

            $table->enum('score_type', ['pasi', 'easi', 'dlqi', 'iga']);
            $table->decimal('value', 6, 2);

            $table->unsignedInteger('recorded_by')->nullable();    // users.id
            $table->timestamp('recorded_at')->nullable();
            $table->timestamps();

            $table->index(['business_id', 'patient_id', 'score_type'], 'derma_score_biz_patient_type_idx');
            $table->foreign('patient_id')->references('id')->on('clc_patients')->onDelete('cascade');
        });
    }

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