<?php

namespace App\Http\Resources;

use App\Models\Expense;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * One expense as the Vue app wants it: raw values plus the icon/tint the server
 * already knows how to resolve. Money and dates are formatted in the browser
 * (resources/js/format.js), so the wire format stays numbers and ISO dates.
 *
 * @property Expense $resource
 */
class ExpenseResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $expense = $this->resource;

        return [
            'id' => $expense->id,
            'expense_date' => $expense->expense_date?->format('Y-m-d'),

            'category_id' => $expense->category_id,
            'category' => $expense->categoryName(),
            'category_icon' => $expense->categoryIcon(),
            'category_tint' => $expense->categoryTint(),

            'description' => $expense->description,

            'payment_method' => $expense->payment_method,
            'payment_icon' => $expense->paymentIcon(),

            'remark' => $expense->remark,
            'amount' => (float) $expense->amount,
        ];
    }
}
