<?php

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

/**
 * Treatment packages & sessions — a shared ClinicCore feature. A clinic sells a
 * multi-session course (e.g. "6× Laser Hair Removal"); assigning it to a patient
 * creates a plan with N session slots, each tracked as scheduled/completed so the
 * patient record shows progress (3/6 done).
 *
 *   clc_treatment_packages       = catalogue/template (business or system-default)
 *   clc_patient_treatment_plans  = a patient's purchased course
 *   clc_treatment_plan_sessions  = each session within a plan
 */
class CreateClcTreatmentPackageTables extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('clc_treatment_packages')) {
            Schema::create('clc_treatment_packages', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id')->nullable();   // null = system template
                $table->unsignedInteger('specialty_id')->nullable();
                $table->string('name', 150);
                $table->text('description')->nullable();
                $table->unsignedSmallInteger('total_sessions')->default(1);
                $table->unsignedSmallInteger('interval_days')->nullable(); // recommended gap between sessions
                $table->decimal('price', 22, 4)->nullable();
                $table->boolean('is_active')->default(true);
                $table->unsignedInteger('sort_order')->default(0);
                $table->timestamps();
                $table->index(['business_id', 'is_active']);
            });
        }

        if (!Schema::hasTable('clc_patient_treatment_plans')) {
            Schema::create('clc_patient_treatment_plans', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id');
                $table->unsignedInteger('clc_patient_id');
                $table->unsignedInteger('package_id')->nullable();
                $table->string('name', 150);
                $table->unsignedSmallInteger('total_sessions')->default(1);
                $table->decimal('price', 22, 4)->nullable();
                $table->enum('status', ['active', 'completed', 'cancelled'])->default('active');
                $table->date('started_at')->nullable();
                $table->text('notes')->nullable();
                $table->unsignedInteger('created_by')->nullable();
                $table->timestamps();
                $table->foreign('clc_patient_id')->references('id')->on('clc_patients')->onDelete('cascade');
                $table->index(['business_id', 'clc_patient_id', 'status'], 'clc_plans_biz_pat_status_idx');
            });
        }

        if (!Schema::hasTable('clc_treatment_plan_sessions')) {
            Schema::create('clc_treatment_plan_sessions', function (Blueprint $table) {
                $table->increments('id');
                $table->unsignedInteger('business_id');
                $table->unsignedInteger('plan_id');
                $table->unsignedSmallInteger('session_no');
                $table->enum('status', ['scheduled', 'completed', 'skipped'])->default('scheduled');
                $table->unsignedInteger('appointment_id')->nullable();
                $table->unsignedInteger('performed_by')->nullable();  // doctor id
                $table->date('performed_at')->nullable();
                $table->text('notes')->nullable();
                $table->unsignedInteger('created_by')->nullable();
                $table->timestamps();
                $table->foreign('plan_id')->references('id')->on('clc_patient_treatment_plans')->onDelete('cascade');
                $table->index(['plan_id', 'status']);
            });
        }
    }

    public function down()
    {
        Schema::dropIfExists('clc_treatment_plan_sessions');
        Schema::dropIfExists('clc_patient_treatment_plans');
        Schema::dropIfExists('clc_treatment_packages');
    }
}
