<?php

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

class AddTimeTrackingToClcAppointments extends Migration
{
    public function up()
    {
        Schema::table('clc_appointments', function (Blueprint $table) {
            if (!Schema::hasColumn('clc_appointments', 'arrived_at')) {
                $table->timestamp('arrived_at')->nullable()->after('status');
            }
            if (!Schema::hasColumn('clc_appointments', 'consultation_started_at')) {
                $table->timestamp('consultation_started_at')->nullable()->after('arrived_at');
            }
            if (!Schema::hasColumn('clc_appointments', 'consultation_ended_at')) {
                $table->timestamp('consultation_ended_at')->nullable()->after('consultation_started_at');
            }
            if (!Schema::hasColumn('clc_appointments', 'waiting_minutes')) {
                $table->unsignedInteger('waiting_minutes')->nullable()->after('consultation_ended_at');
            }
            if (!Schema::hasColumn('clc_appointments', 'consultation_minutes')) {
                $table->unsignedInteger('consultation_minutes')->nullable()->after('waiting_minutes');
            }
        });
    }

    public function down()
    {
        Schema::table('clc_appointments', function (Blueprint $table) {
            foreach (['arrived_at', 'consultation_started_at', 'consultation_ended_at', 'waiting_minutes', 'consultation_minutes'] as $col) {
                if (Schema::hasColumn('clc_appointments', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
}
