<?php

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

/**
 * SIFA P3-T8 — problem-list polish. Additive nullable columns on the existing
 * clc_patient_conditions table (spec §7 problem_list): certainty, structured
 * problem type, and a resolved_date to complement the existing diagnosed_date.
 * The existing status enum (active/chronic/resolved) is kept as-is.
 */
class AddProblemListColumnsToClcPatientConditions extends Migration
{
    public function up()
    {
        Schema::table('clc_patient_conditions', function (Blueprint $table) {
            if (!Schema::hasColumn('clc_patient_conditions', 'certainty')) {
                $table->enum('certainty', ['confirmed', 'probable', 'possible'])->nullable()->after('status');
            }
            if (!Schema::hasColumn('clc_patient_conditions', 'condition_type')) {
                $table->enum('condition_type', ['diagnosis', 'symptom', 'finding', 'condition', 'risk_factor'])
                      ->nullable()->after('certainty');
            }
            if (!Schema::hasColumn('clc_patient_conditions', 'resolved_date')) {
                $table->date('resolved_date')->nullable()->after('diagnosed_date');
            }
        });
    }

    public function down()
    {
        foreach (['certainty', 'condition_type', 'resolved_date'] as $col) {
            if (Schema::hasColumn('clc_patient_conditions', $col)) {
                Schema::table('clc_patient_conditions', function (Blueprint $table) use ($col) {
                    $table->dropColumn($col);
                });
            }
        }
    }
}
