<?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
    {
        // Production Batches - Tracks bowl/batch production
        Schema::create('production_batches', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('product_id')->constrained()->cascadeOnDelete();
            $table->foreignId('recipe_id')->constrained()->cascadeOnDelete();
            $table->foreignId('produced_by')->constrained('users')->cascadeOnDelete();
            $table->string('batch_number')->unique();
            $table->string('batch_size'); // e.g., "5L", "10L"
            $table->decimal('batch_size_value', 10, 2); // numeric value in liters
            $table->integer('total_scoops'); // total scoops this batch can produce
            $table->integer('remaining_scoops'); // scoops left to sell
            $table->integer('sold_scoops')->default(0); // scoops sold
            $table->integer('wasted_scoops')->default(0); // scoops wasted/expired
            $table->decimal('cost_per_scoop', 10, 4)->nullable(); // calculated cost
            $table->enum('status', ['active', 'finished', 'expired', 'cancelled'])->default('active');
            $table->timestamp('produced_at');
            $table->timestamp('expires_at')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index(['branch_id', 'status']);
            $table->index(['product_id', 'status']);
            $table->index('produced_at');
            $table->index('expires_at');
        });

        // Production Batch Ingredients - Records ingredients used in each batch
        Schema::create('production_batch_ingredients', function (Blueprint $table) {
            $table->id();
            $table->foreignId('production_batch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('ingredient_id')->constrained()->cascadeOnDelete();
            $table->decimal('quantity_used', 10, 4);
            $table->string('unit');
            $table->decimal('unit_cost', 10, 4);
            $table->decimal('total_cost', 10, 4);
            $table->timestamps();

            $table->index('production_batch_id');
            $table->index('ingredient_id');
        });

        // Batch Size Configurations - Defines standard batch sizes and their scoop yields
        Schema::create('batch_size_configs', function (Blueprint $table) {
            $table->id();
            $table->string('name'); // "5 Liter Bowl", "10 Liter Bowl"
            $table->string('size_code'); // "5L", "10L"
            $table->decimal('size_value', 10, 2); // 5.00, 10.00
            $table->string('size_unit')->default('L'); // Liters
            $table->integer('scoops_per_batch'); // How many scoops per batch
            $table->decimal('ingredient_multiplier', 10, 4)->default(1.0000); // Recipe multiplier
            $table->integer('default_expiry_hours')->default(48); // Default expiry time
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique('size_code');
        });

        // Scoop Sales Log - Tracks each scoop sale for reporting
        Schema::create('scoop_sales', function (Blueprint $table) {
            $table->id();
            $table->foreignId('production_batch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('order_id')->constrained()->cascadeOnDelete();
            $table->foreignId('order_item_id')->constrained()->cascadeOnDelete();
            $table->foreignId('product_id')->constrained()->cascadeOnDelete();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->integer('scoop_count');
            $table->decimal('sale_price', 10, 2);
            $table->timestamp('sold_at');
            $table->timestamps();

            $table->index(['branch_id', 'sold_at']);
            $table->index(['product_id', 'sold_at']);
            $table->index(['production_batch_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('scoop_sales');
        Schema::dropIfExists('batch_size_configs');
        Schema::dropIfExists('production_batch_ingredients');
        Schema::dropIfExists('production_batches');
    }
};
