<?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('account_payables', function (Blueprint $table) {
            $table->id();
            $table->foreignId('vendor_id')->constrained('vendors');
            $table->string('source_document_type')->comment('來源單據類型 (e.g. goods_receipt)');
            $table->unsignedBigInteger('source_document_id')->comment('來源單據 ID');
            $table->string('document_number')->unique()->comment('應付帳款單號');
            $table->decimal('total_amount', 15, 2)->comment('總金額 (含稅)');
            $table->decimal('tax_amount', 15, 2)->default(0)->comment('稅金');
            $table->string('status')->default('pending')->comment('狀態: pending, partially_paid, paid, cancelled');
            $table->date('due_date')->nullable()->comment('應付日期');
            $table->text('remarks')->nullable()->comment('備註');
            $table->foreignId('created_by')->nullable()->constrained('users');
            $table->timestamps();
            
            $table->index(['source_document_type', 'source_document_id']);
        });
    }

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