<?php

namespace kernel\app_modules\user_stage\services;

use kernel\helpers\Debug;
use kernel\app_modules\user_stage\models\UserStage;
use kernel\FormModel;
use kernel\helpers\Slug;

class UserStageService
{
    public function create(FormModel $form_model): false|UserStage
    {
        $model = new UserStage();
        // Пример заполнения:
        $model->description = $form_model->getItem('description');
        $model->start_date = $form_model->getItem('start_date');
        $model->end_date = $form_model->getItem('end_date');
        $model->title = $form_model->getItem('title');
        $model->position = $form_model->getItem('position');
        $model->status = $form_model->getItem('status');
        $model->user_fields = $form_model->getItem('user_fields');
        $model->slug = Slug::createSlug($form_model->getItem('title'), UserStage::class);

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

        return false;
    }

    public function update(FormModel $form_model, UserStage $user_stage): false|UserStage
    {
        // Пример обновления:
        $user_stage->description = $form_model->getItem('description');
        $user_stage->start_date = $form_model->getItem('start_date');
        $user_stage->end_date = $form_model->getItem('end_date');
        $user_stage->title = $form_model->getItem('title');
        $user_stage->position = $form_model->getItem('position');
        $user_stage->status = $form_model->getItem('status');
        $user_stage->user_fields = $form_model->getItem('user_fields');

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

        return false;
    }

    public static function getStageClass(UserStage $stage): string
    {
        if ($stage->pivot->status === 1){
            return "grey_border";
        }
        elseif ($stage->pivot->status === 2){
            return "blue_border";
        }
        else {
            return "green_border";
        }
    }

    public static function getStageStatusText(UserStage $stage): string
    {
        if ($stage->pivot->status === 1){
            return "Не доступен";
        }
        elseif ($stage->pivot->status === 2){
            return "Открыт";
        }
        else {
            return "Завершен";
        }
    }

    public static function getStageStyle(UserStage $stage): string
    {
        if ($stage->pivot->status === 1){
            return "style='background-color: rgba(255, 193, 7, 0.1); color: #ffc107;'";
        }
        elseif ($stage->pivot->status === 2){
            return "style='background-color: rgba(13, 202, 240, 0.1); color: #0dcaf0;'";
        }
        else {
            return "style='background-color: rgba(25, 135, 84, 0.1); color: #198754;'";
        }
    }
}