<?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
    {
        // Chart of Accounts (hierarchical).
        Schema::create('accounts', function (Blueprint $table) {
            $table->id();
            $table->branchColumn();
            $table->string('code', 20);
            $table->string('name', 150);
            $table->enum('type', ['asset', 'liability', 'equity', 'income', 'expense']);
            $table->foreignId('parent_id')->nullable()->constrained('accounts')->nullOnDelete();
            $table->decimal('opening_balance', 14, 2)->default(0); // signed: debit +, credit -
            $table->boolean('is_active')->default(true)->index();
            $table->boolean('is_system')->default(false); // protected accounts (Cash, Fee Income…)
            $table->string('cash_bank')->nullable();      // 'cash' | 'bank' | null — for cash/bank books
            $table->string('description')->nullable();
            $table->auditColumns();
            $table->timestamps();
            $table->softDeletes();

            $table->unique(['branch_id', 'code']);
        });
    }

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