<?php

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

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

    public function down()
    {
        if (Schema::hasTable('clc_clinic_locations') && Schema::hasColumn('clc_clinic_locations', 'business_location_id')) {
            Schema::table('clc_clinic_locations', function (Blueprint $table) {
                $table->dropForeign(['business_location_id']);
                $table->dropColumn('business_location_id');
            });
        }
    }
};
