<?php

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

/**
 * Before/after clinical photography — a shared ClinicCore feature available to
 * every specialty (dermatology, dental, aesthetics, wound care...). Photos hang
 * off a patient, optionally off an appointment, and carry a treatment phase
 * (baseline / before / after / progress) + body area so the UI can build
 * side-by-side before/after comparisons and a per-area timeline.
 */
class CreateClcClinicalPhotosTable extends Migration
{
    public function up()
    {
        if (Schema::hasTable('clc_clinical_photos')) return;

        Schema::create('clc_clinical_photos', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('business_id');
            $table->unsignedInteger('clc_patient_id');
            $table->unsignedInteger('appointment_id')->nullable();
            $table->unsignedInteger('specialty_id')->nullable();
            $table->enum('phase', ['baseline', 'before', 'after', 'progress', 'other'])->default('progress');
            $table->string('body_area', 80)->nullable();   // e.g. "Face", "Left cheek", "Scalp"
            $table->string('caption', 255)->nullable();
            $table->date('taken_at')->nullable();
            $table->string('file_path', 500);
            $table->string('file_name')->nullable();
            $table->unsignedBigInteger('file_size')->nullable();
            $table->string('mime_type', 100)->nullable();
            $table->unsignedInteger('user_id')->nullable();
            $table->unsignedInteger('sort_order')->default(0);
            $table->timestamps();

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

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