<?php

namespace App\Repositories\Shop;

use App\Models\Shop\Producer;
use App\Repositories\Core\Tag;
use App\Traits\Model\Basic;
use App\Traits\Repository\Imageable;

class Producers
{
    use Basic, Imageable;

    public static function init()
    {
        return [
            'tags_list' => TagGroups::getTreeTags(),
        ];
    }

    public static function autocomplete($str)
    {
        $data = Producer::byAutocomplete($str)->orderBy('name')->limit(30)->get()->pluck('name', 'id');
        $export = [];
        foreach ($data as $key => $name) {
            $export[] = ['value' => $key, 'text' => $name];
        }

        return $export;
    }

    public static function getTags($id)
    {
        return self::get($id)->tags;
    }

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

    public static function getFull($id)
    {
        $producer = self::get($id);
        $data = $producer->toArray();
        $data['tags'] = self::getTagsByProducer($producer);

        return $data;
    }

    public static function getTagsByProducer($producer)
    {
        return Tag::getTagsByModel($producer);
    }

    public static function storeFull($data)
    {
        $images = $data['images'] ?? false;
        $tags = $data['tags'] ?? false;
        unset($data['images']);
        unset($data['tags']);
        $producer = self::store($data);
        self::storeImages($producer, $images);
        self::storeTags($producer, $tags);

        return $producer;
    }

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