<?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::create('customer_opening_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('customer_opening_id');
            $table->unsignedBigInteger('warehouse_id')->nullable();
            $table->string('supplier_info')->nullable();
            $table->decimal('opening_balance', 15, 2);
            $table->string('opening_date');
            $table->unsignedBigInteger('transition_location_id')->nullable(); // or whatever default
            $table->unsignedBigInteger('transition_create_by')->nullable(); // user ID
            $table->unsignedBigInteger('transition_update_by')->nullable(); // user ID
            $table->timestamps();

            // Individual indexes
            $table->index('customer_opening_id');
            $table->index('warehouse_id');

            // Composite indexes
            $table->index(['customer_opening_id', 'warehouse_id']);

            // Foreign key constraints
            $table->foreign('customer_opening_id')->references('id')->on('customer_openings')->onDelete('cascade');
            $table->foreign('warehouse_id')->references('id')->on('warehouses')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('customer_opening_details');
    }
};
