<?php

declare(strict_types=1);

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

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Maestro\Core\Modules\Offers\Models\OfferRow;
use Maestro\Core\Modules\Offers\Services\OfferTotalsService;

/** @property OfferRow $resource */
final class OfferRowResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        [$net] = OfferTotalsService::lineAmounts($this->resource);

        return [
            'id' => $this->id,
            'offer_section_id' => $this->offer_section_id,
            'offer_service_catalog_item_id' => $this->offer_service_catalog_item_id,
            'description_html' => $this->description_html,
            'pricing_type' => $this->pricing_type?->value,
            'custom_unit_label' => $this->custom_unit_label,
            'pricing_label' => OfferTotalsService::pricingLabel($this->resource->pricing_type),
            'quantity' => (float) $this->quantity,
            'unit_price' => (float) $this->unit_price,
            'discount_type' => $this->discount_type?->value,
            'discount_value' => (float) $this->discount_value,
            'vat_rate' => (float) $this->vat_rate,
            'sort_order' => (int) $this->sort_order,
            'line_net_total' => $net,
        ];
    }
}
