<?php

namespace kernel\app_modules\tag\services;

use kernel\app_modules\tag\models\Tag;
use kernel\FormModel;
use kernel\helpers\Slug;

class TagService
{

    public function create(FormModel $form_model): false|Tag
    {
        $model = new Tag();
        $model->label = $form_model->getItem('label');
        $model->entity = $form_model->getItem('entity');
        $model->status = $form_model->getItem('status');
        $model->slug = Slug::createSlug($form_model->getItem('label'), Tag::class);
        if ($model->save()){
            return $model;
        }

        return false;
    }

    public function update(FormModel $form_model, Tag $tag): false|Tag
    {
        if ($tag->label !== $form_model->getItem('label')) {
            $tag->slug = Slug::createSlug($form_model->getItem('label'), Tag::class);
        }
        $tag->label = $form_model->getItem('label');
        $tag->entity = $form_model->getItem('entity');
        $tag->status = $form_model->getItem('status');

        if ($tag->save()){
            return $tag;
        }

        return false;
    }

}