<?php

namespace App\Repositories\Shop;

use App\Models\Botanic\Variety;
use App\Models\Shop\Tag;
use App\Models\Shop\TagGroup;
use App\Traits\Model\Basic;
use Illuminate\Support\Str;

class TagGroups
{
    use Basic;

    public static function getWithTagsAndCountOffers($category_id = false)
    {
        $data = [];
        $tags = Tag::withCountArticlesByCategory($category_id)->get()->toArray();
        dump($tags);
        exit;
        $tagGroups = TagGroup::pluck('name', 'id')->toArray();
        foreach ($tags as $tag) {
            if (! $tag['articles_count']) {
                continue;
            }
            $data[$tag['tag_group_id']]['name'] = $tagGroups[$tag['tag_group_id']];
            $data[$tag['tag_group_id']]['tags'][] = [
                'id' => $tag['id'],
                'name' => $tag['name'],
                'count' => $tag['articles_count'],
            ];
        }

        return $data;
    }

    public static function getWithTagsAndCountOffers2($categoryId = false)
    {
        return Tag::withCount([
            'articles as direct_article_count' => function ($query) use ($categoryId) {
                $query->byCategoryParent($categoryId);
            },
            'articles as indirect_article_count' => function ($query) use ($categoryId) {
                $query->whereHasMorph('product', [Variety::class], function ($subQuery) use ($categoryId) {
                    $subQuery->whereHas('categories', function ($categoryQuery) use ($categoryId) {
                        $categoryQuery->byCategoryParent($categoryId);
                    });
                });
            },
        ])
        ->get()
        ->map(function ($tag) {
            $tag->total_articles = $tag->direct_article_count + $tag->indirect_article_count;
            return $tag;
        })
        ->filter(function ($tag) {
            return $tag->total_articles > 0; // Garde uniquement les tags ayant des articles
        });
    }

    public static function isTagGroupHaveSelected($tagsSelected, $tags)
    {
        foreach ($tags as $tag) {
            if (in_array($tag['id'], $tagsSelected)) {
                return false;
            }
        }

        return true;
    }

    public static function getTreeTags()
    {
        $items = TagGroup::with('tags')->get();
        $tags = [];
        foreach ($items as $group) {
            $group_tags = [];
            foreach ($group->tags as $tag) {
                $group_tags[$tag->id] = $group->name.' - '.$tag->name;
            }
            $tags[] = [
                'label' => $group->name,
                'options' => $group_tags,
            ];
        }

        return $tags;
    }

    public static function getSlug($id)
    {
        return self::get($id)->slug ?? '';
    }

    public static function getName($id)
    {
        return self::get($id)->name ?? '';
    }

    public static function create($data)
    {
        $data['slug'] = Str::slug($data['name']);

        return TagGroup::create($data);
    }

    public static function update($data, $id = false)
    {
        $id = $id ? $id : $data['id'];
        $model = self::get($id);
        if ($data['name'] ?? false) {
            $data['slug'] = Str::slug($data['name']);
        }
        $model->update($data);
        Tags::updateGroup($id, $data['name']);

        return $model;
    }

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

    public static function getModel()
    {
        return TagGroup::query();
    }
}
