<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up()
    {
        Schema::create('journal_entries', function (Blueprint $table) {
            $table->id();
            $table->string('voucher_no')->unique();
            $table->date('transaction_date');
            $table->string('reference_no')->nullable();
            $table->string('narration')->nullable();
            $table->enum('transaction_type', [
                'receipt', 'payment', 'contra', 'journal', 
                'saving_collection', 'loan_disbursement', 'loan_repayment',
                'income', 'expense', 'adjustment'
            ]);
            $table->decimal('total_debit', 15, 2)->default(0);
            $table->decimal('total_credit', 15, 2)->default(0);
            $table->boolean('is_posted')->default(false);
            $table->dateTime('posted_at')->nullable();
            $table->text('remarks')->nullable();
            $table->foreignId('created_by')->constrained('users');
            $table->foreignId('approved_by')->nullable()->constrained('users');
            $table->timestamps();
            $table->softDeletes();
            
            $table->index('transaction_date');
            $table->index('transaction_type');
            $table->index('is_posted');
        });
    }

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