<?php

namespace App\Repositories\Shop;

class Shelves
{
    public static function getOffersByCategoryAndNature($categoryId, $articleNatureId = false, $tags = [], $articleNature = false, $displayByRows = false)
    {
        $articleNatures = Articles::getArticleNaturesWithOffers(['category_id' => $categoryId]);
        $productType = self::getProductType($articleNatureId, $articleNature, $articleNatures);
        $articleNatureId = $articleNatureId ?
            $articleNatureId :
            self::getArticleNatureId($articleNature, $articleNatures);

        return [
            'category' => Categories::getFull($categoryId),
            'breadcrumb' => Categories::getAncestorsByCategory($categoryId),
            'display_by_rows' => $displayByRows,
            'product_type' => $productType,
            'article_nature' => $articleNature,
            'article_natures' => $articleNatures ?? [],
            'product_types' => Articles::getProductTypesWithOffers(['category_id' => $categoryId]),
            'tags_selected' => $tags,
            'articles' => Articles::getArticlesToSell([
                'category_id' => $categoryId,
                'tags' => $tags,
                'product_type' => $productType ?? false,
                'article_nature_id' => $articleNatureId ?? false,
            ]),
            'tags' => TagGroups::getWithTagsAndCountOffers($categoryId),
        ];
    }

    public static function getArticleNatureId($articleNature, $articleNatures = [])
    {
        if (! $articleNature && count($articleNatures)) {
            $articleNature = $articleNatures[0];
        }

        return ArticleNatures::getIdBySlug($articleNature);
    }

    public static function getProductType($articleNatureId, $articleNature, $articleNatures = [])
    {
        if ($articleNatureId) {
            $productType = ArticleNatures::getProductType($articleNatureId);
        } else {
            if (! $articleNature && count($articleNatures)) {
                $articleNature = $articleNatures[0];
            }
            $productType = ArticleNatures::getProductTypeBySlug($articleNature);
        }

        return $productType;
    }
}
