<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            // Make user_id nullable for guest checkout
            if (Schema::hasColumn('orders', 'user_id')) {
                $table->foreignId('user_id')->nullable()->change();
            }
            
            // Guest information
            $table->string('guest_name')->nullable()->after('user_id');
            $table->string('guest_email')->nullable()->after('guest_name');
            $table->string('guest_phone')->nullable()->after('guest_email');
            
            // Review token for guest reviews
            $table->string('review_token', 64)->nullable()->unique()->after('review_requested_at');
            $table->timestamp('reviewed_at')->nullable()->after('review_token');
            
            $table->index('guest_email');
            $table->index('review_token');
        });
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropColumn([
                'guest_name',
                'guest_email', 
                'guest_phone',
                'review_token',
                'reviewed_at',
            ]);
        });
    }
};
