<?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('stock_transfer_outs', function (Blueprint $table) {
            $table->id();
            $table->string('document_no')->unique();
            $table->unsignedInteger('source_farm_id');
            $table->unsignedInteger('destination_farm_id');
            $table->date('transfer_date');
            $table->enum('status', ['pending', 'partially_received', 'completed', 'cancelled', 'transfer_in', 'transfer_out'])->default('pending');
            $table->text('remarks')->nullable();
            $table->unsignedBigInteger('user_id');
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('source_farm_id')->references('id')->on('farms');
            $table->foreign('destination_farm_id')->references('id')->on('farms');
            $table->foreign('user_id')->references('id')->on('users');

            // Indexes
            $table->index('source_farm_id', 'sto_source_farm_idx');
            $table->index('destination_farm_id', 'sto_destination_farm_idx');
            $table->index('status', 'sto_status_idx');
            $table->index('transfer_date', 'sto_transfer_date_idx');
            $table->index(['source_farm_id', 'status'], 'sto_source_farm_status_idx');
            $table->index(['destination_farm_id', 'status'], 'sto_destination_farm_status_idx');
        });
    }

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