<?php

namespace App\Services\Forms\Pages;

use App\Models\Asset;
use App\Models\Objects\FieldType;
use App\Models\Pages\Page;
use App\Models\Pages\PageType;
use App\Models\Registries\RegistryType;
use App\Services\Forms\FormsService;
use App\Transformers\Assets\AssetTransformer;
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', 'title' => 'Основные свойства страницы', 'fields' => $this->commonGroupFields($model),
                'dynamic' => [
                    ['field' => 'type', 'hide' => ['registry_type']],
                    ['field' => 'type', 'value' => PageType::REGISTRY, 'show' => ['registry_type']],
                    //['field' => 'type', 'value' => PageType::PUBLICATIONS, 'show' => ['subtype']]
                ]
            ],
            [
                'name' => 'seo', 'title' => 'Мета данные', 'fields' => $this->seoGroupFields($model)
            ]
        ];
        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,
                'readonly' => !($model->parent_id ?? true),
                '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' => 'picture',
                'title' => 'Изображение',
                'type' => FieldType::IMAGE,
                'value' => ($v = $model->picture ?? null) ? fractal($v, new AssetTransformer()) : null
            ]
        ];
        return ['data' => $fields];
    }

    public function seoGroupFields(?Page $model): array {
        $fields = [
            [
                'name' => 'h1',
                'title' => 'Заголовок H1',
                'type' => FieldType::STRING,
                'value' => $model->h1 ?? null
            ],
            [
                'name' => 'title',
                'title' => 'Title страницы',
                'type' => FieldType::STRING,
                'value' => $model->title ?? null
            ],
            [
                'name' => 'description',
                'title' => 'Описание',
                'type' => FieldType::TEXT,
                'value' => $model->description ?? null
            ],
            [
                'name' => 'keywords',
                'title' => 'Ключевые слова',
                'type' => FieldType::STRING,
                'value' => $model->keywords ?? null
            ],

        ];
        return ['data' => $fields];
    }



    public function store(array $data): ?JsonResponse {
        $parent = Page::byUuid($data['parent'] ?? null)->first();
        $data['parent_id'] = $parent->id ?? 0;
        $data['picture_id'] = ($v = Asset::byUuid($data['picture'] ?? null)->first()) ? $v->id : null;
        $data['slug'] = Str::slug($data['slug'] ?? $data['name'] ?? 'unnamed');
        $model = Page::create($data);
        $model->update(['ord' => $model->getMaxOrd()]);
        $model->checkConflictedProps();
        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['picture_id'] = ($v = Asset::byUuid($data['picture'] ?? null)->first()) ? $v->id : null;
        if ($model->parent_id) $data['slug'] = Str::slug($data['slug'] ?? $data['name'] ?? 'unnamed');
        else unset($data['slug']);
        $model->update($data);
        $model->checkConflictedProps();
        if ($model->type === PageType::REGISTRY) $model->registry->update(['type' => $data['registry_type'] ?? RegistryType::SIMPLE]);
        return fractal($model->fresh(), new PageTransformer())->respond();
    }
}
