<?php

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

/**
 * SIFA P5-T1 (radiology) — radiation-safety dose fields on imaging studies
 * (spec §10 radiology: imaging_study.radiation_dose DAP / DLP / CTDIvol).
 *
 *   dose_dap  — Dose-Area Product (Gy·cm²)      — projection radiography / fluoro
 *   dose_dlp  — Dose-Length Product (mGy·cm)     — CT
 *   ctdivol   — CT Dose Index volume (mGy)        — CT
 *
 * Additive + per-column guarded. The patient imaging history sums the present
 * values for a simple cumulative-dose line.
 */
class AddRadiationDoseToClcImagingStudies extends Migration
{
    public function up()
    {
        if (!Schema::hasTable('clc_imaging_studies')) {
            return;
        }

        Schema::table('clc_imaging_studies', function (Blueprint $table) {
            if (!Schema::hasColumn('clc_imaging_studies', 'dose_dap')) {
                $table->decimal('dose_dap', 12, 4)->nullable()->after('notes');
            }
            if (!Schema::hasColumn('clc_imaging_studies', 'dose_dlp')) {
                $table->decimal('dose_dlp', 12, 4)->nullable()->after('dose_dap');
            }
            if (!Schema::hasColumn('clc_imaging_studies', 'ctdivol')) {
                $table->decimal('ctdivol', 12, 4)->nullable()->after('dose_dlp');
            }
        });
    }

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

        Schema::table('clc_imaging_studies', function (Blueprint $table) {
            foreach (['dose_dap', 'dose_dlp', 'ctdivol'] as $col) {
                if (Schema::hasColumn('clc_imaging_studies', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
}
