<?php

namespace App\Http\Resources;

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

class ProductResource extends JsonResource
{
    public static $wrap = false;

    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $lang = $request->header('Accept-Language') ?? 'ru';

        // get currency from cache
        $currency = cache()->get('currency');

        if ($this->price_discount) {
            $discount_percent = 100 - round($this->price_discount / $this->price * 100);
        } else {
            $discount_percent = 0;
        }

        return [
            'id' => $this->id,
            'name' => $this->name[$lang],
            'price' => ceiling($this->price * $currency->dollar, 100),
            'price_discount' => ceiling($this->price_discount * $currency->dollar, 100),
            'discount_percent' => $discount_percent,
            'is_leader_of_sales' => $this->leader_of_sales,
            'poster' => $this->getPoster(),
            'poster_thumb' => $this->getPosterThumb(),
            'is_favorite' => $this->is_favorite,
            'is_cart' => $this->is_cart,
            'count' => $this->count,
            'power' => $this->power,
        ];
    }
}
