<?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('order_items', function (Blueprint $table) {
            // Add cup size support (nullable for backward compatibility)
            $table->foreignId('cup_size_id')->nullable()->after('product_id')->constrained('cup_sizes')->onDelete('set null');

            // Add flavors JSON column to store multiple flavors with weights
            // Format: [{"product_id": 1, "name": "Vanilla Dream", "weight_grams": 62.5}, ...]
            $table->json('flavors')->nullable()->after('cup_size_id');

            // Add total weight for this item in grams (for weight-based tracking)
            $table->decimal('total_weight_grams', 8, 2)->nullable()->after('flavors');

            // Keep existing product_id for backward compatibility (single flavor items)
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('order_items', function (Blueprint $table) {
            $table->dropForeign(['cup_size_id']);
            $table->dropColumn(['cup_size_id', 'flavors', 'total_weight_grams']);
        });
    }
};
