<?php

namespace app\modules\tag;

use Illuminate\Database\Eloquent\Model;
use itguild\forms\builders\SelectBuilder;
use itguild\forms\builders\TextInputBuilder;
use itguild\forms\inputs\Select;
use kernel\app_modules\tag\models\Tag;
use kernel\app_modules\tag\models\TagEntity;
use kernel\app_modules\tag\services\TagEntityService;
use kernel\helpers\Debug;
use kernel\helpers\Slug;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
use kernel\modules\option\service\OptionService;
use kernel\Request;
use kernel\services\MigrationService;

class TagModule extends Module
{

    public MenuService $menuService;
    public MigrationService $migrationService;

    public function __construct()
    {
        $this->menuService = new MenuService();
        $this->migrationService = new MigrationService();
    }

    /**
     * @throws \Exception
     */
    public function init(): void
    {
        $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag");
        $this->migrationService->runAtPath("{KERNEL_APP_MODULES}/tag/migrations/tag_entity");

        $this->menuService->createItem([
            "label" => "Тэги",
            "url" => "/admin/tag",
            "slug" => "tag",
        ]);

        $this->menuService->createItem([
            "label" => "Тэги",
            "url" => "/admin/settings/tag",
            "slug" => "tag_settings",
            "parent_slug" => "settings"
        ]);

        OptionService::createFromParams("entity_tag_list", "{}", "Список тегов");
    }

    public function deactivate(): void
    {
        $this->menuService->removeItemBySlug("tag");
        $this->menuService->removeItemBySlug("tag_settings");
        OptionService::removeOptionByKey("entity_tag_list");
    }

    public function formInputs(string $entity, Model $model = null): void
    {
        if (isset($model->id)) {
            $value = TagEntityService::getTagsByEntity($entity, $model->id);
        }

        $input = SelectBuilder::build("tag[]", [
            'class' => 'form-control',
            'placeholder' => 'Теги',
            'value' => $value ?? '',
            'multiple' => "multiple",
            'options' => Tag::getTagLabelByEntity($entity)
        ]);
        $input->setLabel("Теги");
        $input->create()->render();
    }

    public function saveInputs(string $entity, Model $model, Request $request): void
    {
        TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete();

        $tags = $request->post("tag");
        if (is_array($tags)) {
            foreach ($tags as $tag) {
                $tagEntity = new TagEntity();
                $tagEntity->entity = $entity;
                $tagEntity->entity_id = $model->id;
                $tagEntity->tag_id = $tag;
                $tagEntity->save();
            }
        }
    }


    public function getItems(string $entity, Model $model): array|string
    {
        $tags = TagEntity::where("entity", $entity)->where("entity_id", $model->id)->with("tag")->get();
        $tagsStr = "";
        foreach ($tags as $tag) {
            $tagsStr .= $tag->tag->label . ", ";
        }

        return substr($tagsStr, 0, -2);
    }

    public function getItem(string $entity, string $entity_id): string
    {
        $tags = TagEntity::where("entity", $entity)->where("entity_id", $entity_id)->get();
        $tagsStr = "";
        foreach ($tags as $tag) {
            $tagsStr .= $tag->tag->label . ", ";
        }

        return substr($tagsStr, 0, -2);
    }

    public function deleteItems(string $entity, Model $model): void
    {
        TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete();
    }
}