<?php

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

return new class extends Migration
{
    /**
     * Registro de cada servicio realizado al cliente. Una entrada por cita atendida.
     */
    public function up(): void
    {
        Schema::create('treatment_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('clinical_history_id')->constrained()->cascadeOnDelete();
            $table->foreignId('booking_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('service_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('employee_id')->nullable()->constrained()->nullOnDelete();

            $table->date('performed_at');
            $table->text('description')->nullable();
            $table->json('products_used')->nullable();
            $table->text('observations')->nullable();
            $table->text('recommendations')->nullable();
            $table->date('next_appointment_suggested')->nullable();

            $table->string('photo_before_path')->nullable();
            $table->string('photo_after_path')->nullable();
            $table->json('additional_photos')->nullable();
            $table->string('client_signature_path')->nullable();

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

            $table->index(['company_id', 'clinical_history_id', 'performed_at'], 'treatment_records_company_history_perf_idx');
        });
    }

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