<?php

namespace App\Services\Forms\Pages;

use App\Models\Objects\FieldType;
use App\Models\Pages\Page;
use App\Models\Pages\PageSubType;
use App\Models\Pages\PageType;
use App\Models\Registries\RegistryType;
use App\Services\Forms\FormsService;
use App\Transformers\Pages\PageTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Str;

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

    public function form(?string $id = null, array $data = []): array {
        $model = Page::byUuid($id)->first();
        $groups = [
            [
                'name' => 'common', 'fields' => $this->commonGroupFields($model),
                'dynamic' => [
                    ['field' => 'type', 'hide' => ['registry_type', 'subtype']],
                    ['field' => 'type', 'value' => PageType::REGISTRY, 'show' => ['registry_type']],
                    ['field' => 'type', 'value' => PageType::PUBLICATIONS, 'show' => ['subtype']]
                ]

            ]
        ];
        return ['title' => $this->formTitle($model), 'data' => $groups];
    }

    public function commonGroupFields(?Page $model): array {
        $fields = [
            [
                'name' => 'name',
                'title' => 'Название',
                'type' => FieldType::STRING,
                'required' => true,
                'value' => $model->name ?? null
            ],
            [
                'name' => 'slug',
                'title' => 'Адрес',
                'type' => FieldType::STRING,
                'hidden' => !$model,
                'value' => $model->slug ?? null
            ],
            [
                'name' => 'type',
                'title' => 'Вид',
                'type' => FieldType::RELATION,
                'required' => true,
                'options' => $this->getRelationItems(PageType::TITLES),
                'value' => $this->getRelationValue(PageType::TITLES, $model->type ?? null)
            ],
            [
                'name' => 'registry_type',
                'title' => 'Вид реестра',
                'type' => FieldType::RELATION,
                'required' => true,
                'options' => $this->getRelationItems(RegistryType::TITLES),
                'value' => $this->getRelationValue(RegistryType::TITLES, $model->registry->type ?? null)
            ],
            [
                'name' => 'subtype',
                'title' => 'Вид публикации',
                'type' => FieldType::RELATION,
                'required' => true,
                'options' => $this->getRelationItems(PageSubType::TITLES),
                'value' => $this->getRelationValue(PageSubType::TITLES, $model->sub_type ?? null)
            ]
        ];
        return ['data' => $fields];
    }



    public function store(array $data): ?JsonResponse {
        $parent = Page::byUuid($data['parent'] ?? null)->first();
        $data['parent_id'] = $parent->id ?? 0;
        $data['slug'] = $data['slug'] ?? Str::slug(Str::transliterate($data['name'] ?? null));
        $model = Page::create($data);
        $model->update(['ord' => $model->getMaxOrd()]);
        if ($model->type === PageType::REGISTRY) $model->registry->update(['type' => $data['registry_type'] ?? RegistryType::SIMPLE]);
        return fractal($model, new PageTransformer())->respond();
    }

    public function update(string $id, array $data): ?JsonResponse {
        $model = Page::byUuid($id)->firstOrFail();
        $data['slug'] = $data['slug'] ?? Str::slug(Str::transliterate($data['name'] ?? null));
        $model->update($data);
        if ($model->type === PageType::REGISTRY) $model->registry->update(['type' => $data['registry_type'] ?? RegistryType::SIMPLE]);
        return fractal($model->fresh(), new PageTransformer())->respond();
    }
}
