<?php

namespace Modules\Accounts\Transformers;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class AccountResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'account_code' => $this->account_code,
            'account_name' => $this->account_name,
            'parent_id' => $this->parent_id,
            'parent' => $this->whenLoaded('parent', fn () => [
                'id' => $this->parent?->id,
                'account_code' => $this->parent?->account_code,
                'account_name' => $this->parent?->account_name,
            ]),
            'account_type' => $this->account_type,
            'is_group' => $this->is_group,
            'is_cash' => $this->is_cash,
            'is_bank' => $this->is_bank,
            'description' => $this->description,
            'default' => $this->default,
            'is_system_generated' => $this->is_system_generated,
            'status' => $this->status,
            'children_count' => $this->children_count,
            'children' => AccountTreeResource::collection($this->whenLoaded('children'))
        ];
    }
}
