<?php

namespace App\Domains\Catalog\Resources;

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

/**
 * @mixin \App\Domains\Catalog\Models\Product
 */
class ProductResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'tenant_codigo' => $this->tenant_codigo,
            'category_id' => $this->categoria_id,
            'brand_id' => $this->brand_id,
            'slug' => $this->slug,
            'nombre' => $this->nombre,
            'descripcion' => $this->descripcion,
            'precio' => $this->precio,
            'category' => $this->whenLoaded('category', fn () =>  $this->category?->nombre),
            'brand' => $this->whenLoaded('brand', fn () => $this->brand?->nombre),
            'images' => $this->whenLoaded('attachments', fn () =>
                $this->attachments
                    ->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
                    ->values()
            ),
            'variants' => ProductVariantResource::collection($this->whenLoaded('variants')),
        ];
    }
}
