<?php

namespace App\Http\Resources;

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

class OrderLineResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'product_id' => $this->product_id,
            'product' => $this->when($this->relationLoaded('product') && $this->product, fn () => [
                'id' => $this->product->id,
                'name' => $this->product->name,
                'metal_type' => $this->product->metal_type,
                'purity' => (float) $this->product->purity,
            ]),
            'metal_type' => $this->metal_type,
            'purity' => $this->purity,
            'grams' => $this->grams,
            'unit_price' => $this->unit_price,
            'total_amount' => $this->total_amount,
            'notes' => $this->notes,
        ];
    }
}
