<?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
    {
        // Stock Ledger - Current stock levels per branch
        Schema::create('stock_ledgers', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('ingredient_id')->constrained()->cascadeOnDelete();
            $table->decimal('quantity', 12, 4)->default(0);
            $table->decimal('reserved_quantity', 12, 4)->default(0); // For pending orders
            $table->decimal('average_cost', 10, 2)->default(0);
            $table->timestamp('last_restocked_at')->nullable();
            $table->timestamps();

            $table->unique(['branch_id', 'ingredient_id']);
            $table->index('branch_id');
            $table->index('ingredient_id');
        });

        // Stock Transactions - All stock movements
        Schema::create('stock_transactions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('ingredient_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
            $table->enum('type', ['in', 'out', 'adjustment', 'transfer', 'waste', 'return']);
            $table->decimal('quantity', 12, 4); // Positive for in, negative for out
            $table->decimal('unit_cost', 10, 2)->nullable();
            $table->decimal('balance_after', 12, 4); // Stock balance after transaction
            $table->string('reference_type')->nullable(); // order, purchase, manual, etc.
            $table->unsignedBigInteger('reference_id')->nullable();
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index('branch_id');
            $table->index('ingredient_id');
            $table->index('type');
            $table->index(['reference_type', 'reference_id']);
            $table->index('created_at');
        });

        // Purchase Orders
        Schema::create('purchase_orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->string('po_number')->unique();
            $table->string('supplier_name')->nullable();
            $table->string('supplier_contact')->nullable();
            $table->enum('status', ['draft', 'pending', 'approved', 'received', 'cancelled'])->default('draft');
            $table->decimal('total_amount', 12, 2)->default(0);
            $table->text('notes')->nullable();
            $table->timestamp('expected_at')->nullable();
            $table->timestamp('received_at')->nullable();
            $table->timestamps();
            $table->softDeletes();

            $table->index('branch_id');
            $table->index('status');
            $table->index('po_number');
        });

        // Purchase Order Items
        Schema::create('purchase_order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('purchase_order_id')->constrained()->cascadeOnDelete();
            $table->foreignId('ingredient_id')->constrained()->cascadeOnDelete();
            $table->decimal('quantity_ordered', 12, 4);
            $table->decimal('quantity_received', 12, 4)->default(0);
            $table->decimal('unit_cost', 10, 2);
            $table->decimal('total_cost', 12, 2);
            $table->timestamps();

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

        // Stock Alerts
        Schema::create('stock_alerts', function (Blueprint $table) {
            $table->id();
            $table->foreignId('branch_id')->constrained()->cascadeOnDelete();
            $table->foreignId('ingredient_id')->constrained()->cascadeOnDelete();
            $table->enum('alert_type', ['low_stock', 'out_of_stock', 'expiring_soon', 'expired']);
            $table->decimal('current_quantity', 12, 4);
            $table->decimal('threshold_quantity', 12, 4);
            $table->boolean('is_resolved')->default(false);
            $table->timestamp('resolved_at')->nullable();
            $table->timestamps();

            $table->index('branch_id');
            $table->index('is_resolved');
            $table->index('alert_type');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('stock_alerts');
        Schema::dropIfExists('purchase_order_items');
        Schema::dropIfExists('purchase_orders');
        Schema::dropIfExists('stock_transactions');
        Schema::dropIfExists('stock_ledgers');
    }
};
