<?php

namespace App\Modules\Inventory\Resources;

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

class ProductResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'category' => $this->whenLoaded('category', fn () => $this->category ? [
                'id' => $this->category->id,
                'name' => $this->category->name,
            ] : null),
            'name' => $this->name,
            'sku' => $this->sku,
            'barcode' => $this->barcode,
            'purchase_price' => $this->purchase_price,
            'selling_price' => $this->selling_price,
            'current_stock' => $this->current_stock,
            'low_stock_threshold' => $this->low_stock_threshold,
            'is_low_stock' => (float) $this->current_stock <= (float) $this->low_stock_threshold,
            'unit' => $this->unit,
            'status' => $this->status,
        ];
    }
}
