<?php

namespace Modules\Product\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,
            'name' => $this->name,
            'slug' => $this->slug,
            'description' => $this->description,
            'price' => (float)$this->price,
            'unit' => $this->unit,
            'unit_price' => $this->unit_price ? (float)$this->unit_price : null,
            'image_url' => $this->image_url,
            'brand' => $this->brand,
            'discount' => $this->discount ? (float)$this->discount : null,
            'original_price' => $this->original_price ? (float)$this->original_price : null,
            'final_price' => round((float)$this->final_price, 2),
            'is_on_sale' => $this->is_on_sale,
            'nutri_score' => $this->nutri_score,
            'tags' => $this->tags,
            'nutrition' => $this->nutrition,
            'preparation' => $this->preparation,
            'ingredients' => $this->ingredients,
            'allergens_info' => $this->allergens_info,
            'stock_quantity' => $this->stock_quantity,
            'min_order_quantity' => $this->min_order_quantity,
            'max_order_quantity' => $this->max_order_quantity,
            'is_active' => $this->is_active,
            'is_save_the_food' => $this->is_save_the_food,
            'is_weekly_special' => $this->is_weekly_special,
            'category_id' => $this->category_id,
            'category' => $this->whenLoaded('category', fn() => [
                'id' => $this->category->id,
                'name' => $this->category->name,
                'slug' => $this->category->slug,
                'icon' => $this->category->icon,
                'image_url' => $this->category->image_url,
            ]),
            'media' => $this->whenLoaded('media', fn() => $this->media->map(fn($m) => [
                'id' => $m->id,
                'url' => $m->path ? url('storage/' . $m->path) : null,
                'filename' => $m->filename,
                'mime_type' => $m->mime_type,
            ])
            ),
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];
    }
}
