<?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('purchase_returns', function (Blueprint $table) {
            $table->id();
            $table->string('code')->unique()->comment('退回單號');
            $table->unsignedBigInteger('vendor_id')->comment('廠商 ID');
            $table->unsignedBigInteger('warehouse_id')->comment('退貨出庫倉庫 ID');
            $table->unsignedBigInteger('user_id')->comment('建立者 ID');
            $table->date('return_date')->comment('退貨日期');
            
            $table->string('status', 20)->default('draft')->comment('狀態: draft, completed, cancelled');
            
            $table->decimal('total_amount', 12, 2)->default(0)->comment('總金額 (不含稅)');
            $table->decimal('tax_amount', 12, 2)->default(0)->comment('稅金');
            $table->decimal('grand_total', 12, 2)->default(0)->comment('總計含稅金額');
            
            $table->text('remarks')->nullable()->comment('備註');
            
            $table->timestamps();
            $table->softDeletes();
            
            // 由於 Modular Monolith 規範，外部模組的 Foreign Key 不一定建立實體約束以避免牽一髮動全身
            // $table->foreign('vendor_id')->references('id')->on('vendors')->onDelete('restrict');
            $table->index('code');
            $table->index('vendor_id');
            $table->index('warehouse_id');
            $table->index('status');
        });
    }

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