<?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
    {
        Schema::create('stock_closings', function (Blueprint $table) {
            $table->id();
            $table->foreignId('warehouse_id')->nullable()->constrained('warehouses')->nullOnDelete();
            $table->enum('closing_type', ['weekly', 'monthly']);
            $table->date('closing_date');
            $table->date('opening_date')->nullable(); // Opening date for next period
            $table->integer('opening_quantity')->default(0);
            $table->decimal('opening_amount', 12, 2)->default(0);
            $table->integer('closing_quantity')->default(0);
            $table->decimal('closing_amount', 12, 2)->default(0);
            $table->integer('purchases_quantity')->default(0); // New items purchased
            $table->decimal('purchases_amount', 12, 2)->default(0);
            $table->integer('transfers_out_quantity')->default(0); // Stock transferred out
            $table->decimal('transfers_out_amount', 12, 2)->default(0);
            $table->foreignId('closed_by')->constrained('users')->cascadeOnDelete();
            $table->enum('status', ['draft', 'closed'])->default('draft');
            $table->text('notes')->nullable();
            $table->timestamps();
            
            $table->index('warehouse_id');
            $table->index('closing_date');
            $table->index('closing_type');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('stock_closings');
    }
};

