<?php

namespace App\Http\Resources;

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

class ProductResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'company_id' => $this->company_id,
            'category_id' => $this->category_id,
            'tax_rate_id' => $this->tax_rate_id,
            'unit_id' => $this->unit_id,
            'name' => $this->name,
            'slug' => $this->slug,
            'sku' => $this->sku,
            'barcode' => $this->barcode,
            'description' => $this->description,
            'image_url' => $this->image_url,
            'type' => $this->type,
            'cost_price' => $this->cost_price,
            'selling_price' => $this->selling_price,
            'wholesale_price' => $this->wholesale_price,
            'min_selling_price' => $this->min_selling_price,
            'reorder_level' => $this->reorder_level,
            'reorder_quantity' => $this->reorder_quantity,
            'track_stock' => $this->track_stock,
            'allow_negative_stock' => $this->allow_negative_stock,
            'is_active' => $this->is_active,
            'is_featured' => $this->is_featured,
            'meta' => $this->meta,
            'category' => $this->whenLoaded('category', fn () => $this->category?->only(['id', 'name', 'slug'])),
            'tax_rate' => $this->whenLoaded('taxRate', fn () => $this->taxRate?->only(['id', 'name', 'rate'])),
            'unit' => $this->whenLoaded('unit', fn () => $this->unit?->only(['id', 'name', 'abbreviation'])),
        ];
    }
}
