<?php

namespace kernel\app_modules\user_custom_fields\services;

use itguild\forms\builders\SelectBuilder;
use itguild\forms\builders\TextInputBuilder;
use kernel\app_modules\user_custom_fields\models\CustomField;
use kernel\app_modules\user_custom_fields\models\forms\CreateCustomFieldForm;
use kernel\FormModel;

class CustomFieldService
{
    public function create(FormModel $form_model): false|CustomField
    {
        $model = new CustomField();
        // Пример заполнения:
        $model->slug = $form_model->getItem('slug');
        $model->label = $form_model->getItem('label');
        $model->type = $form_model->getItem('type');
        $model->entity = $form_model->getItem('entity');
        $model->field_options = $form_model->getItem('field_options');
        $model->status = $form_model->getItem('status');

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

        return false;
    }

    public function update(FormModel $form_model, CustomField $custom_field): false|CustomField
    {
        // Пример обновления:
        $custom_field->slug = $form_model->getItem('slug');
        $custom_field->label = $form_model->getItem('label');
        $custom_field->type = $form_model->getItem('type');
        $custom_field->entity = $form_model->getItem('entity');
        $custom_field->field_options = $form_model->getItem('field_options');
        $custom_field->status = $form_model->getItem('status');

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

        return false;
    }

    public static function getCustomFields()
    {
        $model = CustomField::where(['entity' => 'user'])->where(['status' => CustomField::ACTIVE_STATUS])->get();

        return $model;
    }

    public static function getList(): array
    {
        return CustomField::select('id', 'label')->get()
            ->pluck('label', 'id')
            ->toArray();

    }

    public static function getCustomFieldHtml(CustomField $field, int $userId)
    {
        $value = UserCustomValuesService::getValueByFieldAndUser($field->id, $userId);

        if ($field->type === "string"){
            $input = TextInputBuilder::build($field->slug, [
                'class' => 'form-control',
                'placeholder' => $field->label,
                'value' => $value->value ?? '',
            ]);
        }
        else {
            $options = explode(", ", $field->field_options);
            $options = array_combine($options, $options);
            $input = SelectBuilder::build($field->slug, [
                'class' => 'form-control',
                'placeholder' => $field->label,
                'value' => $value->value ?? '',
            ])->setOptions($options);
        }
        $input->setLabel($field->label);

        return $input->create()->fetch();
    }

    public static function getOrCreateBySlug(string $slug): CustomField
    {
        $model = CustomField::where('slug', $slug)->first();
        if (!$model) {
            $form = new CreateCustomFieldForm();
            $service = new self();
            $form->load([
                'slug' => $slug,
                'label' => $slug,
                'entity' => 'user',
                'type' => 'string',
            ]);
            $model = $service->create($form);
        }

        return $model;
    }
}