<?php

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

/**
 * P5-T3 dermatology deep-dive — per-lesion clinical register on a shared
 * ClinicCore patient (1:many), optionally tied to a clc appointment. Drives the
 * body-map editor, ABCDE skin-cancer checklist and the per-lesion photo
 * comparison strip (photo_ids reference clc_clinical_photos rows — the shared
 * ClinicCore before/after photography feature we reuse rather than re-build).
 *
 * Additive + guarded: no-ops when ClinicCore isn't installed and never touches
 * a core table.
 */
class CreateDermaLesionsTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('derma_lesions')) return;
        if (! Schema::hasTable('clc_patients')) return; // requires ClinicCore

        Schema::create('derma_lesions', 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('body_region', [
                'head', 'face', 'neck', 'chest', 'back', 'abdomen',
                'left_arm', 'right_arm', 'left_hand', 'right_hand',
                'left_leg', 'right_leg', 'left_foot', 'right_foot',
                'genital', 'other',
            ]);
            // Position on the region diagram (0-100 percent of width/height).
            $table->unsignedTinyInteger('position_x')->nullable();
            $table->unsignedTinyInteger('position_y')->nullable();

            $table->enum('morphology', [
                'macule', 'patch', 'papule', 'plaque', 'nodule', 'tumor',
                'vesicle', 'bulla', 'pustule', 'wheal', 'scar', 'ulcer', 'other',
            ]);
            $table->decimal('size_mm', 6, 1)->nullable();
            $table->string('color', 100)->nullable();
            $table->enum('border', ['well_defined', 'ill_defined', 'irregular'])->nullable();
            $table->enum('evolution', ['stable', 'growing', 'shrinking', 'new', 'recurrent'])->nullable();

            // ABCDE skin-cancer checklist: {asymmetry,border,color,diameter,evolving} booleans.
            $table->json('abcde_flags')->nullable();
            $table->text('dermoscopy_notes')->nullable();

            $table->boolean('biopsy_taken')->default(false);
            $table->enum('biopsy_type', ['shave', 'punch', 'excisional', 'incisional'])->nullable();

            $table->enum('status', ['active', 'resolved', 'excised', 'monitoring'])->default('active');

            // clc_clinical_photos ids linked to this lesion (chronological strip).
            $table->json('photo_ids')->nullable();

            $table->unsignedInteger('created_by')->nullable(); // users.id
            $table->timestamps();

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

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