<?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('journal_entry_lines', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('tenant_id');
            $table->unsignedBigInteger('journal_entry_id');
            $table->smallInteger('line_no')->comment('행번호');
            $table->string('dc_type', 10)->comment('차대구분: debit/credit');
            $table->string('account_code', 10)->comment('계정코드');
            $table->string('account_name', 100)->comment('계정명');
            $table->unsignedBigInteger('trading_partner_id')->nullable()->comment('거래처ID');
            $table->string('trading_partner_name', 100)->nullable()->comment('거래처명');
            $table->bigInteger('debit_amount')->default(0)->comment('차변금액');
            $table->bigInteger('credit_amount')->default(0)->comment('대변금액');
            $table->string('description', 300)->nullable()->comment('적요');
            $table->timestamps();

            $table->foreign('journal_entry_id')
                ->references('id')
                ->on('journal_entries')
                ->onDelete('cascade');

            $table->index(['journal_entry_id', 'line_no']);
            $table->index(['tenant_id', 'account_code']);
        });
    }

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