<?php

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

/**
 * Adds optional location_id to vc_doctor_schedules so a doctor can have
 * different schedules at different locations (e.g. Hossam: Mon-Wed at
 * Cairo branch, Thu-Fri at Heliopolis branch).
 *
 * Nullable so existing single-location clinics don't need migration —
 * NULL means "any location" (BookingSlotResolver treats null-location
 * schedules as a wildcard match).
 *
 * Owned by PortalApi so we don't touch VetClinic uncommitted state.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vc_doctor_schedules')) return;
        if (Schema::hasColumn('vc_doctor_schedules', 'location_id')) return;

        Schema::table('vc_doctor_schedules', function (Blueprint $t) {
            $t->unsignedBigInteger('location_id')->nullable()->after('department_id')->index();
        });
    }

    public function down(): void
    {
        if (! Schema::hasTable('vc_doctor_schedules')) return;
        if (! Schema::hasColumn('vc_doctor_schedules', 'location_id')) return;

        Schema::table('vc_doctor_schedules', function (Blueprint $t) {
            $t->dropColumn('location_id');
        });
    }
};
