<?php

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

/**
 * Add the human vital-sign ("constants") fields the clinic vitals grid was
 * missing: blood pressure, SpO2, blood glucose and BMI. Additive + idempotent.
 */
class AddConstantsToClcAppointments extends Migration
{
    public function up()
    {
        Schema::table('clc_appointments', function (Blueprint $table) {
            if (!Schema::hasColumn('clc_appointments', 'systolic_bp')) {
                $table->unsignedSmallInteger('systolic_bp')->nullable()->after('respiratory_rate');
            }
            if (!Schema::hasColumn('clc_appointments', 'diastolic_bp')) {
                $table->unsignedSmallInteger('diastolic_bp')->nullable()->after('systolic_bp');
            }
            if (!Schema::hasColumn('clc_appointments', 'spo2')) {
                $table->unsignedTinyInteger('spo2')->nullable()->after('diastolic_bp');
            }
            if (!Schema::hasColumn('clc_appointments', 'blood_glucose')) {
                $table->decimal('blood_glucose', 6, 1)->nullable()->after('spo2');
            }
            if (!Schema::hasColumn('clc_appointments', 'bmi')) {
                $table->decimal('bmi', 5, 1)->nullable()->after('blood_glucose');
            }
        });
    }

    public function down()
    {
        Schema::table('clc_appointments', function (Blueprint $table) {
            foreach (['systolic_bp', 'diastolic_bp', 'spo2', 'blood_glucose', 'bmi'] as $col) {
                if (Schema::hasColumn('clc_appointments', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
}
