<?php

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

/**
 * Lets the Next Visit panel carry DB-driven type & reason multi-selects forward
 * into the auto-scheduled follow-up appointment.
 */
class AddNextVisitTypeReasonIdsToVcAppointments extends Migration
{
    public function up()
    {
        Schema::table('vc_appointments', function (Blueprint $table) {
            if (!Schema::hasColumn('vc_appointments', 'next_visit_type_ids')) {
                $table->json('next_visit_type_ids')->nullable()->after('next_visit_reason');
            }
            if (!Schema::hasColumn('vc_appointments', 'next_visit_reason_ids')) {
                $table->json('next_visit_reason_ids')->nullable()->after('next_visit_type_ids');
            }
        });
    }

    public function down()
    {
        Schema::table('vc_appointments', function (Blueprint $table) {
            foreach (['next_visit_type_ids', 'next_visit_reason_ids'] as $col) {
                if (Schema::hasColumn('vc_appointments', $col)) $table->dropColumn($col);
            }
        });
    }
}
