<?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('store_requisitions', function (Blueprint $table) {
            $table->id();
            $table->string('doc_no')->unique()->comment('單號 SR-YYYYMMDD-XX');
            $table->unsignedBigInteger('store_warehouse_id')->comment('申請倉庫（任意類型）');
            $table->unsignedBigInteger('supply_warehouse_id')->nullable()->comment('供貨倉庫（審核時填入）');
            $table->enum('status', ['draft', 'pending', 'approved', 'rejected', 'completed', 'cancelled'])
                  ->default('draft');
            $table->text('remark')->nullable()->comment('申請備註');
            $table->text('reject_reason')->nullable()->comment('駁回原因');
            $table->unsignedBigInteger('created_by')->comment('申請人');
            $table->unsignedBigInteger('approved_by')->nullable()->comment('審核人');
            $table->timestamp('submitted_at')->nullable()->comment('提交時間');
            $table->timestamp('approved_at')->nullable()->comment('審核時間');
            $table->unsignedBigInteger('transfer_order_id')->nullable()->comment('關聯調撥單');
            $table->timestamps();

            $table->index('status');
            $table->index('store_warehouse_id');
            $table->index('created_by');
        });
    }

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