<?php

namespace App\Services\Forms\Pages;

use App\Models\Asset;
use App\Models\Localization\Locale;
use App\Models\Objects\FieldType;
use App\Models\Pages\Page;
use App\Models\Pages\PageType;
use App\Services\Forms\FormsService;
use App\Transformers\Assets\AssetTransformer;
use App\Transformers\Localization\LocaleTransformer;
use App\Transformers\Pages\PageTransformer;
use Illuminate\Http\JsonResponse;
use Spatie\Fractal\Fractal;

class LocalePageForms 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)],
            ['name' => 'seo', 'title' => 'Мета данные', 'fields' => $this->seoGroupFields($model)]
        ];
        return ['title' => $this->formTitle($model), 'data' => $groups];
    }

    public function commonGroupFields(?Page $model): array {
        $fields = [
            [
                'name' => 'locale',
                'title' => 'Язык',
                'type' => FieldType::RELATION,
                'required' => true,
                'options' => $this->getLocales()
            ],
            [
                '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 getLocales(): Fractal {
        return fractal(Locale::query()->notDefault()->doesntHave('page')->get(), new LocaleTransformer());
    }



    public function store(array $data): ?JsonResponse {
        $locale = Locale::byUuid($data['locale'] ?? null)->firstOrFail();
        $data['picture_id'] = ($v = Asset::byUuid($data['picture'] ?? null)->first()) ? $v->id : null;
        $data += ['type' => PageType::CONTENT, 'name' => $locale->title, 'slug' => $locale->name, 'ord' => Page::query()->where(['parent_id' => 0])->max('ord') + 1];
        $model = Page::query()->where(['parent_id' => 0, 'slug' => $locale->name])->withTrashed()->first() ?? Page::create($data);
        $model->restore();
        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;
        $model->update($data);
        return fractal($model->fresh(), new PageTransformer())->respond();
    }
}
