<?php

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

return new class extends Migration
{
    public function up(): void
    {
        // Scoop Weight Configuration Table
        Schema::create('scoop_weight_configs', function (Blueprint $table) {
            $table->id();
            $table->integer('scoop_count'); // 1, 2, 3, 4, 5, 6
            $table->decimal('total_weight_grams', 8, 2); // Total weight for this scoop count
            $table->decimal('weight_per_scoop_grams', 8, 2)->nullable(); // Calculated: total / count
            $table->decimal('min_weight_grams', 8, 2)->nullable(); // Tolerance range minimum
            $table->decimal('max_weight_grams', 8, 2)->nullable(); // Tolerance range maximum
            $table->boolean('is_active')->default(true);
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->unique('scoop_count');
            $table->index('is_active');
        });

        // Insert default scoop weight configurations
        DB::table('scoop_weight_configs')->insert([
            [
                'scoop_count' => 1,
                'total_weight_grams' => 70.00,
                'weight_per_scoop_grams' => 70.00,
                'min_weight_grams' => 65.00,
                'max_weight_grams' => 75.00,
                'is_active' => true,
                'notes' => 'Single scoop - generous portion',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'scoop_count' => 2,
                'total_weight_grams' => 120.00,
                'weight_per_scoop_grams' => 60.00,
                'min_weight_grams' => 115.00,
                'max_weight_grams' => 125.00,
                'is_active' => true,
                'notes' => 'Double scoop - standard serving',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'scoop_count' => 3,
                'total_weight_grams' => 170.00,
                'weight_per_scoop_grams' => 56.67,
                'min_weight_grams' => 165.00,
                'max_weight_grams' => 175.00,
                'is_active' => true,
                'notes' => 'Triple scoop - popular choice',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'scoop_count' => 4,
                'total_weight_grams' => 220.00,
                'weight_per_scoop_grams' => 55.00,
                'min_weight_grams' => 215.00,
                'max_weight_grams' => 225.00,
                'is_active' => true,
                'notes' => 'Quad scoop - large serving',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'scoop_count' => 5,
                'total_weight_grams' => 265.00,
                'weight_per_scoop_grams' => 53.00,
                'min_weight_grams' => 260.00,
                'max_weight_grams' => 270.00,
                'is_active' => true,
                'notes' => 'Five scoop - extra large',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'scoop_count' => 6,
                'total_weight_grams' => 310.00,
                'weight_per_scoop_grams' => 51.67,
                'min_weight_grams' => 300.00,
                'max_weight_grams' => 320.00,
                'is_active' => true,
                'notes' => 'Six scoop - maximum serving',
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);

        // Add scoop_weight_grams column to scoop_sales table for actual weight tracking
        Schema::table('scoop_sales', function (Blueprint $table) {
            $table->decimal('scoop_weight_grams', 8, 2)->nullable()->after('scoop_count');
            $table->integer('total_scoops_in_order')->nullable()->after('scoop_weight_grams');
            $table->text('weight_calculation_notes')->nullable()->after('total_scoops_in_order');
        });
    }

    public function down(): void
    {
        Schema::table('scoop_sales', function (Blueprint $table) {
            $table->dropColumn(['scoop_weight_grams', 'total_scoops_in_order', 'weight_calculation_notes']);
        });

        Schema::dropIfExists('scoop_weight_configs');
    }
};
