<?php

namespace App\Repositories\Shop;

use App\Models\Shop\Article;
use App\Models\Shop\Merchandise;

class ArticleCategories
{
    public static function getProductTypeByCategory($category_id)
    {
        $models = self::getProductTypesModelsByCategory($category_id);

        return ($models[0] ?? false) === Merchandise::class ? 'merchandise' : 'botanic';
    }

    public static function getProductTypesModelsByCategory($category_id)
    {
        return Article::byCategory($category_id)->select('product_type')->distinct()->get();
    }

    public static function countProductTypesByCategory($category_id)
    {
        return Article::byCategory($category_id)->select('product_type')->distinct()->count();
    }

    public static function getByCategory($category_id)
    {
        return Article::byCategory($category_id)->with(['prices', 'product', 'image'])->get();
    }

    public static function getCategoriesByArticle($article)
    {
        return $article->categories->pluck('id')->toArray();
    }

    public static function getCategoriesNameByArticle($article)
    {
        return $article->categories->pluck('name', 'id')->toArray();
    }

    public static function storeCategories($article, $categories)
    {
        if (! $categories) {
            return false;
        }
        $categories = collect($categories)->transform(
            function ($item, $key) {
                return (int) $item;
            }
        )->toArray();

        return $article->syncCategories($categories, true);
    }
}
