<?php

namespace App\Http\Resources\Admin\Silver;

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

class SilverIndexResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $data =  [
            'id' => $this->id,
            'country' => $this->Country?->{'name_' . $request->header('lang')},
            'name' => $this->silver->{'name_' . $request->header('lang')},
            'icon' => uploadsPath($this->silver->icon),

            'base_price' => $this->base_price,
            'dollar_price' => $this->dollar_price,
            'status_price' => $this->status_price,
            'status' => (bool) $this->status,
            'ordering' =>  $this->ordering,
            'change_amount' => $this->change_amount ? number_format($this->change_amount, 1) : '0',
            'latest_updated' => Carbon::parse($this->latest_updated)->format('Y-m-d H:i:s'),


        ];

         if ($this->country_id == 1) {
            $data['buy_price'] = is_numeric($this->buy_price) ? rtrim(rtrim(number_format($this->buy_price, 2, '.', ''), '0'), '.') : $this->buy_price;
            $data['sell_price'] = is_numeric($this->sell_price) ? rtrim(rtrim(number_format($this->sell_price, 2, '.', ''), '0'), '.') : $this->sell_price;
        }

        return $data;
    }
}
