<?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('chart_of_accounts', function (Blueprint $table) {
            $table->id();
            $table->string('account_code')->unique();
            $table->string('account_name');
            $table->enum('account_type', ['asset', 'liability', 'equity', 'revenue', 'expense']);
            $table->enum('account_subtype', [
                // Assets
                'current_asset', 'fixed_asset', 'bank_account', 'cash_in_hand', 'receivable',
                // Liabilities
                'current_liability', 'long_term_liability', 'payable', 'loan_payable',
                // Equity
                'capital', 'retained_earning', 'drawing',
                // Revenue
                'membership_fee', 'monthly_saving', 'loan_interest', 'fine', 'other_income',
                // Expense
                'salary', 'rent', 'utility', 'office_expense', 'meeting_expense', 'other_expense'
            ]);
            $table->decimal('opening_balance', 15, 2)->default(0);
            $table->decimal('current_balance', 15, 2)->default(0);
            $table->integer('parent_id')->nullable();
            $table->integer('level')->default(1);
            $table->boolean('is_active')->default(true);
            $table->boolean('is_system_account')->default(false);
            $table->text('description')->nullable();
            $table->timestamps();
            $table->softDeletes();
            
            $table->index('account_type');
            $table->index('account_subtype');
            $table->index('parent_id');
        });
    }

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