<?php

namespace App\Services\Forms\Objects;

use App\Events\FeedbackSender;
use App\Models\Objects\Field;
use App\Models\Objects\FieldsGroup;
use App\Models\Objects\FieldType;
use App\Services\Forms\FormsService;
use App\Transformers\Objects\FieldTransformer;

class FieldForms extends FormsService
{
    public array $formTitles = ['create' => 'Создание поля', 'update' => 'Редактирование поля'];

    public function form(?string $id = null, array $data = []): array {
        $model = Field::byUuid($id)->first();
        $groups = [
            ['name' => 'common', 'fields' => $this->commonGroupFields($model)]
        ];
        return ['title' => $this->formTitle($model), 'data' => $groups];
    }

    public function commonGroupFields(?Field $model): array {
        $fields = [
            [
                'name' => 'name',
                'title' => 'Наименование',
                'type' => FieldType::STRING,
                'required' => true,
                'value' => $model->name ?? null
            ],
            [
                'name' => 'title',
                'title' => 'Заголовок',
                'type' => FieldType::STRING,
                'required' => true,
                'value' => $model->title ?? null
            ],
            [
                'name' => 'type',
                'title' => 'Тип данных',
                'type' => FieldType::RELATION,
                'required' => true,
                'options' => $this->getRelationItems(FieldType::TITLES),
                'value' => $this->getRelationValue(FieldType::TITLES, $model->type ?? null)

            ],
            [
                'name' => 'required',
                'title' => 'Обязательное',
                'type' => FieldType::BOOLEAN,
                'value' => $model->required ?? false
            ],
            [
                'name' => 'multiple',
                'title' => 'Множественое',
                'type' => FieldType::BOOLEAN,
                'value' => $model->multiple ?? false
            ],
            [
                'name' => 'filterable',
                'title' => 'Фильтр',
                'type' => FieldType::BOOLEAN,
                'value' => $model->filterable ?? false
            ],
            [
                'name' => 'hidden',
                'title' => 'Скрытое',
                'type' => FieldType::BOOLEAN,
                'value' => $model->hidden ?? false
            ],
            [
                'name' => 'params',
                'title' => 'Параметры',
                'type' => FieldType::STRING,
                'value' => $model->relared ?? null
            ],
        ];
        return ['data' => $fields];
    }

    public function store(array $data) {
        $group = FieldsGroup::byUuid($data['group'])->first();
        $model = Field::create($data);
        $group->fields()->attach($model->id);
        return fractal($model, new FieldTransformer())->respond();
    }

    public function update(string $id, array $data) {
        $model = Field::byUuid($id)->firstOrFail();
        $model->update($data);
        return fractal($model->fresh(), new FieldTransformer())->respond();
    }
}