<?php

namespace App\Services\Forms\Registries;

use App\Models\Asset;
use App\Models\Objects\FieldType;
use App\Models\Registries\Category;
use App\Models\Registries\Entry;
use App\Models\Registries\Registry;
use App\Services\Forms\FormsService;
use App\Transformers\Assets\AssetTransformer;
use App\Transformers\Registries\EntryTransformer;
use Illuminate\Http\JsonResponse;

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

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

    public function commonGroupFields(?Entry $model): array {
        $fields = [
            [
                'name' => 'number',
                'title' => 'Номер записи',
                'type' => FieldType::STRING,
                'required' => true,
                'value' => $model->number ?? null
            ],
            [
                'name' => 'name',
                'title' => 'Название',
                'type' => FieldType::STRING,
                'required' => true,
                'value' => $model->name ?? null
            ],
            [
                'name' => 'asset',
                'title' => 'Документ',
                'type' => FieldType::DOCUMENT,
                'required' => true,
                'value' => ($asset = $model->asset ?? null) ? fractal($asset, new AssetTransformer()) : null
            ],
            [
                'name' => 'active_since',
                'title' => 'Дата начала действия',
                'type' => FieldType::DATE,
                '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
            ]
        ];
        return ['data' => $fields];
    }



    public function store(array $data): ?JsonResponse {
        $registry = Registry::byUuid($data['registry'] ?? null)->firstOrFail();
        $category = Category::byUuid($data['category'] ?? null)->first();
        $data['asset_id'] = ($asset = Asset::byUuid($data['asset'])->first()) ? $asset->id : null;
        $data['category_id'] = $category->id ?? 0;
        $model = $registry->entries()->create($data);
        return fractal($model, new EntryTransformer())->respond();
    }

    public function update(string $id, array $data): ?JsonResponse {
        $model = Entry::byUuid($id)->firstOrFail();
        $data['asset_id'] = ($asset = Asset::byUuid($data['asset'])->first()) ? $asset->id : null;
        $model->update($data);
        return fractal($model->fresh(), new EntryTransformer())->respond();
    }
}
