<?php

namespace kernel\app_modules\card\services;

use kernel\app_modules\card\models\Card;
use kernel\app_modules\card\models\CardTemplate;
use kernel\FormModel;

class CardTemplateService
{

    public function create(FormModel $form_model): false|CardTemplate
    {
        $model = new CardTemplate();
        // Пример заполнения:
        $model->path = $form_model->getItem('path');
        $model->status = $form_model->getItem('status');
        $model->title = $form_model->getItem('title');
        // $model->slug = Slug::createSlug($form_model->getItem('title'), Card::class); // Генерация уникального slug

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

        return false;
    }

    public function update(FormModel $form_model, CardTemplate $cardTemplate): false|CardTemplate
    {
        // Пример обновления:
        $cardTemplate->title = $form_model->getItem('title');
        $cardTemplate->path = $form_model->getItem('path') ?? $cardTemplate->path;
        $cardTemplate->status = $form_model->getItem('status');

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

        return false;
    }

    public static function getTemplatesList(): array
    {
        $arr = [];
        foreach (CardTemplate::all()->toArray() as $cardTemplate){
            $arr[$cardTemplate['id']] = $cardTemplate['title'];
        }
        if (!empty($arr)) {
            return $arr;
        }

        return [];
    }

}