<?php

namespace App\Repositories\Botanic;

use App\Models\Botanic\Specie;
use App\Repositories\Core\Tag;
use App\Repositories\Shop\TagGroups;
use App\Traits\Model\Basic;
use App\Traits\Repository\Imageable;

class Species
{
    use Basic, Imageable;

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

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

    public static function getTags($id)
    {
        $model = self::get($id) ?? false;

        return $model ? $model->tags->toArray() : false;
    }

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

        return $data;
    }

    public static function getTagsBySpecie($specie)
    {
        return Tag::getTagsByModel($specie);
    }

    public static function get($id)
    {
        return Specie::with('tags.tag_group')->find($id);
    }

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

        return $specie;
    }

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

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