<?php

namespace TPS\API\Http\Resources\Checkout;

use Illuminate\Http\Resources\Json\JsonResource;
use Webkul\RestApi\Http\Resources\V1\Shop\Customer\CustomerAddressResource;
use Webkul\Tax\Helpers\Tax;

class CartResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request): array
    {

        return [
            'id'                                 => $this->id,
            'shipping_method'                    => $this->shipping_method,
            'coupon_code'                        => $this->coupon_code,
            'is_gift'                            => $this->is_gift,
            'items_count'                        => (int) $this->items_count,
            'items_qty'                          => (int)$this->items_qty,
            'grand_total'                        => (double) $this->grand_total,
            'formatted_grand_total'               => core()->formatPrice($this->base_grand_total, $this->cart_currency_code),
            'sub_total'                          => (double) $this->sub_total,
            'formatted_sub_total'                 => core()->formatPrice($this->base_sub_total, $this->cart_currency_code),
            'discount'                           => (double) $this->discount_amount,
            'formatted_discount'                  => core()->formatPrice($this->base_discount_amount, $this->cart_currency_code),
            'checkout_method'                    => $this->checkout_method,
            'vendors'                            => $this->groupByVendors($this->items),
            'selected_shipping_rate'             => new CartShippingRateResource($this->selected_shipping_rate),
            'payment'                            => new CartPaymentResource($this->payment),
            'billing_address'                    => new CustomerAddressResource($this->billing_address),
            'shipping_address'                   => new CustomerAddressResource($this->shipping_address),
            'formatted_discounted_sub_total'      => core()->formatPrice($this->base_sub_total - $this->base_discount_amount, $this->cart_currency_code),

        ];
    }

    /**
     * Format tax amounts.
     *
     * @param  array  $taxes
     * @param  bool  $isBase
     * @return array
     */
    private function formatTaxAmounts(array $taxes, bool $isBase = false): array
    {
        $result = [];

        foreach ($taxes as $taxRate => $taxAmount) {
            if ($isBase === true) {
                $result[$taxRate] = core()->formatBasePrice($taxAmount);
            } else {
                $result[$taxRate] = core()->formatPrice($taxAmount, $this->cart_currency_code);
            }
        }

        return $result;
    }

    private function groupByVendors($items){
        $data = array();
        foreach($items as $item){
//            $seller = $this->sellerProductRepository->getSellerByProductId($item->product_id);
            $data['Elektronika'][] = CartItemResource::make($item);
        }
        return $data;
    }
}
