<?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('inventory_transfer_orders', function (Blueprint $table) {
            $table->id();
            $table->string('doc_no')->unique();
            $table->foreignId('from_warehouse_id')->constrained('warehouses')->cascadeOnDelete();
            $table->foreignId('to_warehouse_id')->constrained('warehouses')->cascadeOnDelete();
            $table->string('status')->default('draft'); // draft, completed, voided
            $table->string('remarks')->nullable();
            
            // 審核/建立資訊
            $table->timestamp('posted_at')->nullable(); // 過帳時間
            $table->foreignId('created_by')->constrained('users'); 
            $table->foreignId('updated_by')->nullable()->constrained('users');
            $table->foreignId('posted_by')->nullable()->constrained('users');
            
            $table->timestamps();
        });

        Schema::create('inventory_transfer_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('transfer_order_id')->constrained('inventory_transfer_orders')->cascadeOnDelete();
            $table->foreignId('product_id')->constrained('products');
            $table->string('batch_number')->nullable();
            $table->decimal('quantity', 10, 2); 
            $table->string('notes')->nullable();
            $table->timestamps();
        });
    }

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