<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Invoices\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Maestro\Core\Modules\Invoices\Models\InvoiceLine;

/** @mixin InvoiceLine */
class InvoiceLineResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'sort_order' => $this->sort_order,
            'item_id' => $this->item_id,
            'description' => $this->description,
            'quantity' => (float) $this->quantity,
            'unit_price_ex_vat' => (float) $this->unit_price_ex_vat,
            'vat_rate_id' => $this->vat_rate_id,
            'vat_percent' => $this->whenLoaded('vatRate', fn (): float => (float) $this->vatRate->rate_percent),
            'line_total_ex_vat' => (float) $this->line_total_ex_vat,
            'line_vat_amount' => (float) $this->line_vat_amount,
            'line_total_inc_vat' => (float) $this->line_total_inc_vat,
        ];
    }
}
