<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('consultations', function (Blueprint $table) {
            // Make user_id nullable for guest consultations
            $table->foreignId('user_id')->nullable()->change();

            // Add guest fields after user_id
            $table->string('guest_name', 255)->nullable()->after('user_id');
            $table->string('guest_email', 255)->nullable()->after('guest_name');
            $table->string('guest_phone', 50)->nullable()->after('guest_email');

            // Index for 1-per-day guest lookup performance
            $table->index('guest_email');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('consultations', function (Blueprint $table) {
            $table->dropIndex(['guest_email']);
            $table->dropColumn(['guest_name', 'guest_email', 'guest_phone']);
            // Note: Cannot safely restore NOT NULL if guest records exist
        });
    }
};
