<?php

namespace App\Services\Forms\Registries;

use App\Models\Objects\NirObject;
use App\Models\Objects\ObjectType;
use App\Models\Registries\Entry;
use App\Services\Forms\FormsService;
use App\Transformers\Objects\FieldTransformer;
use App\Transformers\Objects\ObjectPropertyTransformer;
use App\Transformers\Objects\ObjectTransformer;
use Illuminate\Http\JsonResponse;

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

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

    public function commonGroupFields(?NirObject $model): array {
        return $model ? fractal($model->groups->first()->fields, new ObjectPropertyTransformer($model))->toArray() :
            fractal(ObjectType::byName('entry-operation')->first()->groups()->first()->fields, new FieldTransformer())->toArray();

        /*
        $fields = [
            [
                'name' => 'type',
                'title' => 'Вид работы',
                'type' => FieldType::RELATION,
                'required' => true,
                'appearance' => 'radio',
                'options' => $this->getRelationItems(OperationType::TITLES),
                'value' => $this->getRelationValue(OperationType::TITLES, $model->type ?? null)
            ],
            [
                'name' => 'order_name',
                'title' => 'Наименование приказа',
                'type' => FieldType::STRING,
                'required' => true,
                'value' => $model->order_name ?? null
            ],
            [
                'name' => 'order_date',
                'title' => 'Дата приказа',
                'required' => true,
                'type' => FieldType::DATE,
                'value' => ($v = $model->order_date ?? null) ? $v->toIso8601String() : null
            ],
            [
                'name' => 'order',
                'title' => 'Документ приказа',
                'type' => FieldType::DOCUMENT,
                'required' => true,
                'value' => ($order = $model->order ?? null) ? fractal($order, new AssetTransformer()) : null
            ],
            [
                'name' => 'listing',
                'title' => 'Вхождение в перечень ПП',
                'type' => FieldType::RELATION,
                'multiple' => true,
                'appearance' => 'checkbox',
                'options' => fractal(Dictionary::byName('listings')->first()->items, new DictionaryItemTransformer()),
                'value' => null
            ],
            [
                'name' => 'active_since',
                'title' => 'Дата начала действия',
                'type' => FieldType::DATE,
                'required' => true,
                'value' => ($v = $model->active_since ?? null) ? $v->toIso8601String() : null
            ],
            [
                'name' => 'active_till',
                'title' => 'Дата окончания действия',
                'type' => FieldType::DATE,
                'value' => ($v = $model->active_till ?? null) ? $v->toIso8601String() : null
            ],
            [
                'name' => 'developer',
                'title' => 'Разработчик',
                'type' => FieldType::STRING,
                'required' => true,
                'value' => $model->developer ?? null
            ]
        ];
        return ['data' => $fields];
        */
    }



    public function store(array $data): ?JsonResponse {
        $entry = Entry::byUuid($data['entry'] ?? null)->firstOrFail();
        $model = $entry->createObject('entry-operation', null, 'operations');
        $model->setValues($data);
        return fractal($model, new ObjectTransformer())->respond();
    }

    public function update(string $id, array $data): ?JsonResponse {
        $model = NirObject::byUuid($id)->firstOrFail();
        $model->setValues($data);
        return fractal($model->fresh(), new ObjectTransformer())->respond();
    }
}
