<?php

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

/**
 * P5-T2 dental deep-dive — chart upgrades. Additive, guarded columns on the
 * existing dental_tooth_findings table. Storage stays universal 1-32; the new
 * notation_system column only records the dentist's preferred display notation
 * (conversion is a pure display helper on DentalToothFinding). The existing
 * tooth chart keeps working: every column is nullable/defaulted and every add
 * is guarded by Schema::hasColumn.
 *
 * The `condition` column already exists as string(30); its stored palette is
 * widened in DentalToothFinding::CONDITIONS (healthy, carious, filled, crowned,
 * bridge_abutment, missing, implant, root_canal + legacy aliases) — no enum to
 * alter since it was created as a plain string, so no data migration needed.
 */
class ExtendDentalToothFindings extends Migration
{
    public function up()
    {
        if (! Schema::hasTable('dental_tooth_findings')) {
            return; // requires the base dental migration
        }

        Schema::table('dental_tooth_findings', function (Blueprint $table) {
            if (! Schema::hasColumn('dental_tooth_findings', 'notation_system')) {
                // Display-only. Storage of tooth_number always stays universal 1-32.
                $table->enum('notation_system', ['universal', 'fdi', 'palmer'])->nullable()->after('tooth_number');
            }
            if (! Schema::hasColumn('dental_tooth_findings', 'probing_depth_mm')) {
                $table->unsignedTinyInteger('probing_depth_mm')->nullable()->after('note');
            }
            if (! Schema::hasColumn('dental_tooth_findings', 'bleeding_on_probing')) {
                $table->boolean('bleeding_on_probing')->nullable()->after('probing_depth_mm');
            }
            if (! Schema::hasColumn('dental_tooth_findings', 'mobility')) {
                // Miller mobility grade 0-3.
                $table->unsignedTinyInteger('mobility')->nullable()->after('bleeding_on_probing');
            }
            if (! Schema::hasColumn('dental_tooth_findings', 'endo_status')) {
                $table->enum('endo_status', ['none', 'treated', 'retreated', 'pending'])->nullable()->after('mobility');
            }
            if (! Schema::hasColumn('dental_tooth_findings', 'implant_note')) {
                $table->string('implant_note', 255)->nullable()->after('endo_status');
            }
        });
    }

    public function down()
    {
        if (! Schema::hasTable('dental_tooth_findings')) {
            return;
        }

        Schema::table('dental_tooth_findings', function (Blueprint $table) {
            foreach (['notation_system', 'probing_depth_mm', 'bleeding_on_probing', 'mobility', 'endo_status', 'implant_note'] as $col) {
                if (Schema::hasColumn('dental_tooth_findings', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
}
