<?php

namespace App\Repositories\Shop;

use App\Models\Shop\Category;
use App\Repositories\Core\Categories as CategoryTrees;
use App\Repositories\Core\Tag;

class Shelves
{
    public static function getOffersByCategoryAndNature($categoryId, $articleNatureId = false, $tags = [], $articleNature = false, $displayByRows = false)
    {
        $productTypes = Articles::getProductTypesWithOffers([
            'category_id' => $categoryId,
        ]);

        $articleNatures = Articles::getArticleNaturesWithOffers([
            'category_id' => $categoryId,
        ]);

        if ($articleNatureId) {
            $productType = ArticleNatures::getProductType($articleNatureId);
        } else {
            if (! $articleNature) {
                if (count($articleNatures)) {
                    $articleNature = $articleNatures[0];
                }
            }
            $productType = ArticleNatures::getProductTypeBySlug($articleNature);
            $articleNatureId = ArticleNatures::getIdBySlug($articleNature);
        }
        return [
            'category' => Categories::getFull($categoryId),
            'breadcrumb' => Categories::getAncestorsByCategory($categoryId),
            'display_by_rows' => $displayByRows,
            'product_type' => $productType,
            'article_nature' => $articleNature,
            'article_natures' => $articleNatures ?? [],
            'product_types' => $productTypes ?? [],
            'tags_selected' => $tags,
            'articles' => Articles::getArticlesToSell([
                'category_id' => $categoryId,
                'tags' => $tags,
                'product_type' => $productType ?? false,
                'article_nature_id' => $articleNatureId ?? false,
            ]),
            'tags' => TagGroups::getWithTagsAndCountOffers($categoryId),
        ];
    }
}
