<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Modules\Accounts\Enums\AccountTypeEnum;
use Modules\Core\Enums\StatusEnum;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('chart_of_accounts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('account_code')->unique();
            $table->string('account_name');
            $table->foreignId('parent_id')->nullable()->constrained('chart_of_accounts')->nullOnDelete();
            $table->boolean('is_group')->default(true);
            $table->enum('account_type', AccountTypeEnum::casesAsArray());
            $table->boolean('is_cash')->default(false);
            $table->boolean('is_bank')->default(false);
            $table->text('description')->nullable();
            $table->boolean('default')->default(false);
            $table->boolean('is_system_generated')->default(false);
            $table->enum('status', StatusEnum::casesAsArray())->default(StatusEnum::ACTIVE);

            $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
            $table->foreignId('updated_by')->nullable()->constrained('users')->nullOnDelete();

            $table->timestamps();
            $table->softDeletes();

            $table->index(['parent_id', 'account_type']);
            $table->index(['account_type', 'is_group']);
            $table->index(['account_type', 'account_code']);
            $table->index(['is_bank', 'status']);
        });
    }

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