<?php

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

return new class extends Migration
{
    /**
     * Archivos adjuntos a la ficha del cliente: fotos, recetas, autorizaciones,
     * formatos firmados, evidencias.
     */
    public function up(): void
    {
        Schema::create('client_files', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('company_id')->constrained()->cascadeOnDelete();
            $table->foreignId('clinical_history_id')->constrained()->cascadeOnDelete();
            $table->foreignId('booking_id')->nullable()->constrained()->nullOnDelete();
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();

            $table->enum('type', ['photo', 'document', 'prescription', 'authorization', 'evidence', 'other'])
                  ->default('other');
            $table->string('title')->nullable();
            $table->text('description')->nullable();

            $table->string('path');           // ruta privada en storage
            $table->string('original_name');
            $table->string('mime_type', 100);
            $table->unsignedBigInteger('size_bytes')->default(0);

            $table->boolean('is_private')->default(true);
            $table->json('tags')->nullable();

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

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

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