<?php

namespace App\Http\Resources\Transaction;

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

class FuturesTransaction extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        if($this->created_at instanceof Carbon) {
            $date = $this->created_at->format('Y-m-d H:i:s');
        } else {
            $date = $this->created_at;
        }

        $order = [
            'id' => $this->id,
            'is_long' => $this->is_long,
            'market' => $this->market->name,
            'created_at' => $date,
            'quantity' => $this->quantity,
            'balance' => $this->balance,
            'released_amount' => $this->released_amount,
            'price' => $this->price,
            'leverage' => intval($this->leverage),
            'liquidation_price' => $this->liquidation_price,
            'type' => $this->is_long ? 'long' : 'short',
            'pnl' => $this->pnl,
            'pnl_profitable' => $this->pnl >= 0,
            'status' => $this->status
        ];

        if($this->custom_fields && is_array($this->custom_fields)) {
            $order = array_merge($order, $this->custom_fields);
        }

        return $order;
    }
}
