<?php

namespace App\Repositories\Shop;

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

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

    public static function getFull($id, $sale_channel_id = false)
    {
        $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID();
        $offer = Offer::with([
            'article.article_nature',
            'article.product',
            'tariff' => function ($query) use ($sale_channel_id) {
                $query->bySaleChannel($sale_channel_id);
            },
            'tariff.price_lists' => function ($query) use ($sale_channel_id) {
                $query->BySaleChannel($sale_channel_id);
            },
            'tariff.price_lists.price_list_values',
            'variation',
        ])->find($id);
        $images = Articles::getFullImagesByArticle($offer->article);
        $offer->article->image = Articles::getPreviewSrc($images[0] ?? false);
        return $offer;
    }

    public static function getPrice($id, $quantity = 1, $sale_channel_id = false)
    {
        $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID();
        $offer = Offer::withPriceBySaleChannelByQuantity($sale_channel_id, $quantity)->find($id);
        return $offer->price_lists->first()->price_list_values->first();
    }

    public static function getBasket($sale_channel_id = false)
    {
        $sale_channel_id = $sale_channel_id ? $sale_channel_id : 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($sale_channel_id)
        ->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 = Offer::with(['article'])->findOrFail($id);
        return [
            'id' => $id,
            'name' => $offer->article->name,
            'price' => self::getPrice($id, $quantity)->price_taxed,
            'quantity' => $quantity,
            'attributes' => [],
        ];
    }

    public static function getOffersByArticles($article_ids, $sale_channel_id = false)
    {
        return self::getOffersBySaleChannelRaw($sale_channel_id)->byArticles($article_ids)->get();
    }

    public static function getOffersByArticle($article_id, $sale_channel_id = false)
    {
        return self::getOffersBySaleChannelRaw($sale_channel_id)->byArticle($article_id)->get();
    }

    public static function getOffersBySaleChannel($sale_channel_id = false)
    {
        return self::getOffersBySaleChannelRaw($sale_channel_id)->get();
    }

    public static function getOffersBySaleChannelRaw($sale_channel_id = false)
    {
        $sale_channel_id = $sale_channel_id ? $sale_channel_id : SaleChannels::getDefaultID();
        return Offer::active()->byStockAvailable()
            ->with([
                'article_nature',
                'variation',
                'tariff.price_lists' => function ($query) use ($sale_channel_id) {
                    $query->bySaleChannel($sale_channel_id);
                },
                'tariff.price_lists.price_list_values',
            ])
            ->bySaleChannel($sale_channel_id);
    }

    public static function getThumbSrcById($id)
    {
        return self::getThumbSrc(self::get($id));
    }

    public static function getThumbSrc(Offer $offer)
    {
        $image = $offer->article ? Articles::getFullImageByArticle($offer->article) : false;
        return $image ? Articles::getThumbSrc($image) : false;
    }
    
    public static function getLast()
    {
        return Offer::with(['article.image'])->active()->orderByDesc('updated_at')->get();
    }

    public static function getByCategoryWithTags($category_id)
    {
        $category = Categories::get($category_id);
        $tags = Categories::getTagsByCategory($category);
        $offers1 = self::getByCategory($category_id)->toArray();
        $offers2 = self::getByTags($tags)->toArray();
        $data = array_merge($offers1, $offers2);
        return $data;
    }

    public static function getByCategory($category_id)
    {
        return Offer::with(['article.image'])->byCategory($category_id)->get();
    }

    public static function getByTags($tags)
    {
        return Offer::with(['article.tags'])->byTags($tags)->get();
    }

    public static function getAll()
    {
        return Offer::get();
    }

    public static function get($id)
    {
        return Offer::findOrFail($id);
    }

    public static function store($data)
    {
        $id = isset($data['id']) ? $data['id'] : false;
        $item = $id ? self::update($data, $id) : self::create($data);
        return $item->id;
    }

    public static function create($data)
    {
        return Offer::create($data);
    }

    public static function update($data, $id = false)
    {
        $id = $id ? $id : $data['id'];
        $item = self::get($id);
        $item->update($data);
        return $item;
    }

    public static function destroy($id)
    {
        return Offer::destroy($id);
    }

    public static function toggle_active($id, $status_id)
    {
        return self::update(['status_id' => $status_id], $id);
    }
}
