<?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('products', function (Blueprint $table) {
            // Only add columns that don't exist yet

            if (!Schema::hasColumn('products', 'scoop_capacity')) {
                $table->integer('scoop_capacity')->nullable()->after('unit_of_measure')
                    ->comment('Number of scoops this container holds (for ice cream products)');
            }

            if (!Schema::hasColumn('products', 'has_inner_options')) {
                $table->boolean('has_inner_options')->default(false)->after('is_active')
                    ->comment('Whether this product allows inner option selections (flavours, toppings, etc.)');
            }

            if (!Schema::hasColumn('products', 'price_per_scoop')) {
                $table->decimal('price_per_scoop', 10, 2)->nullable()->after('scoop_price')
                    ->comment('Price per scoop for ice cream products (replaces scoop_price)');
            }

            if (!Schema::hasColumn('products', 'metadata')) {
                $table->json('metadata')->nullable()->after('description')
                    ->comment('Additional product metadata (container_type, weight info, etc.)');
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $columns = ['scoop_capacity', 'has_inner_options', 'price_per_scoop', 'metadata'];

            foreach ($columns as $column) {
                if (Schema::hasColumn('products', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
