<?php

namespace App\Modules\Accounting\Resources;

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

class ExpenseResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'category' => $this->whenLoaded('category', fn (): ?array => $this->category ? [
                'id' => $this->category->id,
                'name' => $this->category->name,
            ] : null),
            'financial_account' => $this->whenLoaded('financialAccount', fn (): ?array => $this->financialAccount ? [
                'id' => $this->financialAccount->id,
                'name' => $this->financialAccount->name,
                'type' => $this->financialAccount->type,
            ] : null),
            'expense_number' => $this->expense_number,
            'expense_date' => optional($this->expense_date)->format('Y-m-d'),
            'description' => $this->description,
            'amount' => $this->amount,
            'payment_method' => $this->payment_method,
            'vendor' => $this->vendor,
            'reference' => $this->reference,
            'notes' => $this->notes,
            'status' => $this->status,
        ];
    }
}
