<?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('tooth_records', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained()->restrictOnDelete();
            $table->foreignId('patient_id')->constrained()->cascadeOnDelete();
            $table->unsignedTinyInteger('tooth');           // FDI 11–48
            $table->json('surfaces')->nullable();           // ["M","O"]; NULL = whole tooth
            $table->string('condition', 32);                // config('dental.conditions') key
            $table->string('state', 20)->default('existing'); // existing|planned|completed
            $table->string('note', 500)->nullable();
            $table->foreignId('treatment_plan_item_id')->nullable()
                ->constrained('treatment_plan_items')->nullOnDelete();
            $table->foreignId('recorded_by')->nullable()->constrained('users')->nullOnDelete();
            $table->timestamp('recorded_at');
            $table->timestamps();
            $table->index(['tenant_id', 'patient_id', 'tooth', 'recorded_at']);
        });
    }

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