<?php

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

class ClcAddPreferredDoctorToOwnersAndPets extends Migration
{
    public function up()
    {
        if (Schema::hasTable('clc_patient_contact_details') && !Schema::hasColumn('clc_patient_contact_details', 'preferred_doctor_id')) {
            Schema::table('clc_patient_contact_details', function (Blueprint $table) {
                $table->unsignedInteger('preferred_doctor_id')->nullable()->after('referred_by')->index();
            });
        }

        // Per-pet override of the owner's general preferred doctor
        if (Schema::hasTable('clc_patients') && !Schema::hasColumn('clc_patients', 'preferred_doctor_id')) {
            Schema::table('clc_patients', function (Blueprint $table) {
                $table->unsignedInteger('preferred_doctor_id')->nullable()->after('serial_no')->index();
            });
        }
    }

    public function down()
    {
        if (Schema::hasColumn('clc_patient_contact_details', 'preferred_doctor_id')) {
            Schema::table('clc_patient_contact_details', function (Blueprint $table) {
                $table->dropColumn('preferred_doctor_id');
            });
        }
        if (Schema::hasColumn('clc_patients', 'preferred_doctor_id')) {
            Schema::table('clc_patients', function (Blueprint $table) {
                $table->dropColumn('preferred_doctor_id');
            });
        }
    }
}
