<?php

namespace App\Repositories\Shop;

use App\Repositories\Core\Tag;

class ArticleTags
{
    public static function getTagsByArticle($article)
    {
        return $article->tags->pluck('id')->toArray();
    }

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

    public static function getTagsSlugByArticle($article)
    {
        return $article->tags->pluck('slug', 'id')->toArray();
    }

    public static function getFullTagsSlugByArticle($article)
    {
        $data = [];

        switch ($article->product_type) {
            case 'App\Models\Botanic\Variety':
                $data += $article->product->tags->toArray();
                if ($article->product->specie ?? false) {
                    $data += $article->product->specie->tags->toArray();
                }
                break;
            case 'App\Models\Botanic\Specie':
                $data += $article->product->tags->toArray();
                break;
            case 'App\Models\Shop\Merchandise':
                $data += $article->product->tags->toArray();
                $data += $article->product->producer->tags->toArray();
                break;
            default:
        }
        $data += $article->tags->toArray();

        foreach ($data as $tag) {
            if (! isset($tags[$tag['group']][$tag['name']])) {
                $tags[$tag['group']][] = $tag['name'];
            }
        }

        return $tags ?? null;
    }

    public static function storeTags($article, $tags)
    {
        return Tag::storeTags($article, $tags);
    }
}
