<?php

namespace App\Repositories\Shop;

use App\Models\Shop\Offer;
use App\Repositories\Core\User\ShopCart;

class Baskets
{
    public static function addBasket($offer_id, $quantity = 1, $update = false)
    {
        if (ShopCart::has($offer_id) && ! $quantity) {
            $ret = ShopCart::remove($offer_id);
        }
        $data = $quantity ? self::getBasketData($offer_id, $quantity) : false;

        return $data ? ShopCart::add($data, $update) : false;
    }

    public static function getBasketSummary($saleChannelId = false, $deliveryTypeId = false)
    {
        $basket = ShopCart::getContent();
        $offers = Offer::with('variation')->whereIn('id', ShopCart::keys())->get();
        $total = 0;
        $totalTaxed = 0;
        $totalWeight = 0;
        foreach ($basket as $item) {
            $offer = $offers->where('id', $item->id)->first();
            $prices = Offers::getPrice($item->id, $item->quantity, $saleChannelId);
            $weight = $offer->weight * $item->quantity;
            $detail[] = self::getRowDetail($item, $offer, $prices, $weight);
            $total += self::getTotal($item->quantity, $prices->price);
            $totalTaxed += self::getTotal($item->quantity, $prices->price_taxed);
            $totalWeight += $weight;
        }
        $shipping = 0;
        $data = [
            'detail' => $detail,
            'total' => $total,
            'taxes' => $totalTaxed - $total,
            'total_taxed' => $totalTaxed,
            'total_weight' => $totalWeight,
            'shipping' => $shipping,
            'total_shipped' => $totalTaxed + $shipping,
        ];

        return $data ?? false;
    }

    public static function getRowDetail($item, $offer, $prices, $weight)
    {
        return [
            'offer_id' => (int) $item->id,
            'name' => $offer->article->name.' ('.$offer->variation->name.')',
            'quantity' => (int) $item->quantity,
            'price' => (float) $prices->price,
            'tax' => $prices->price_taxed - $prices->price,
            'price_taxed' => (float) $prices->price_taxed,
            'total' => self::getTotal($item->quantity, $prices->price),
            'total_tax' => self::getTotal($item->quantity, $prices->price_taxed - $prices->price),
            'total_taxed' => self::getTotal($item->quantity, $prices->price_taxed),
            'weight' => $weight,
        ];
    }

    public static function getWeight()
    {
        $basket = ShopCart::getContent();
        $offers = Offer::with('variation')->byIds(ShopCart::keys())->get();
        $totalWeight = 0;
        foreach ($basket as $item) {
            $offer = $offers->where('id', $item->id)->first();
            $weight = $offer->weight * $item->quantity;
            $totalWeight += $weight;
        }
        
        return $totalWeight;
    }

    public static function getTotal($quantity, $price)
    {
        return $quantity * $price;
    }

    public static function getBasket($saleChannelId = false)
    {
        $saleChannelId = $saleChannelId ? $saleChannelId : SaleChannels::getDefaultID();
        $basket = ShopCart::getContent();
        // dump($basket->toArray());
        $offers = Offer::with([
            'variation',
            'article.article_nature',
            'article.product.Specie',
            'article.image',
            'price_lists.price_list_values',
        ])->withPriceListsBySaleChannel($saleChannelId)
            ->whereIn('id', ShopCart::keys())->get();
        foreach ($basket as $item) {
            $offer = $offers->where('id', $item->id)->first();
            $article_nature = strtolower($offer->article->article_nature->name);
            $data[$article_nature][] = [
                'id' => (int) $item->id,
                'name' => $item->name,
                'quantity' => (int) $item->quantity,
                'price' => $item->price,
                'variation' => $offer->variation->name,
                'image' => Articles::getPreviewSrc(Articles::getFullImageByArticle($offer->article)),
                'latin' => $offer->article->product->specie->latin ?? false,
            ];
        }

        return $data ?? false;
    }

    public static function getBasketData($id, $quantity = 1)
    {
        $offer = Offers::get($id, ['article', 'variation']);

        return [
            'id' => $id,
            'name' => self::getArticleName($offer),
            'price' => Offers::getPrice($id, $quantity)->price_taxed,
            'quantity' => $quantity,
            'attributes' => [],
        ];
    }

    public static function getArticleName(&$offer)
    {
        return $offer->article->name;
        // return $offer->article->name . ' (' . $offer->variation->name . ')';
    }
}
