<?php

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

/**
 * An institution's canonical area (and therefore local authority). Care settings
 * referencing the institution pre-fill their area from it; null keeps existing
 * institutions unchanged.
 */
return new class extends Migration {
    public function up(): void
    {
        Schema::table('institutions', function (Blueprint $table) {
            if (! Schema::hasColumn('institutions', 'area_id')) {
                $table->foreignId('area_id')->nullable()->after('display_name')
                    ->constrained()->nullOnDelete();
            }
        });
    }

    public function down(): void
    {
        Schema::table('institutions', function (Blueprint $table) {
            $table->dropConstrainedForeignId('area_id');
        });
    }
};
