<?php

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

class AddLocationIdToClcDepartments extends Migration
{
    public function up()
    {
        if (Schema::hasTable('clc_departments') && !Schema::hasColumn('clc_departments', 'location_id')) {
            Schema::table('clc_departments', function (Blueprint $table) {
                $table->unsignedInteger('location_id')->nullable()->after('business_id');
                $table->foreign('location_id')->references('id')->on('clc_clinic_locations')->onDelete('set null');
            });
        }
    }

    public function down()
    {
        if (Schema::hasColumn('clc_departments', 'location_id')) {
            Schema::table('clc_departments', function (Blueprint $table) {
                $table->dropForeign(['location_id']);
                $table->dropColumn('location_id');
            });
        }
    }
}
