<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('patient_files', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('tenant_id')->constrained()->restrictOnDelete();
            $table->foreignId('patient_id')->constrained()->cascadeOnDelete();
            $table->string('type', 20); // photo_before|photo_after|xray|other
            $table->string('path');
            $table->string('thumb_path')->nullable();
            $table->string('original_name', 190);
            $table->string('mime', 100);
            $table->unsignedBigInteger('size');     // bytes
            $table->json('teeth')->nullable();      // FDI tags: [16, 26]
            $table->string('caption', 190)->nullable();
            // Before/after pairing: the AFTER photo points at its BEFORE photo.
            $table->foreignId('paired_with_id')->nullable()
                ->constrained('patient_files')->nullOnDelete();
            $table->foreignId('treatment_plan_id')->nullable()
                ->constrained('treatment_plans')->nullOnDelete();
            $table->foreignId('appointment_id')->nullable()
                ->constrained('appointments')->nullOnDelete();
            $table->foreignId('uploaded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamps();
            $table->softDeletes();
            $table->index(['tenant_id', 'patient_id', 'type']);
        });
    }

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