<?php

namespace App\Services\Forms\Publications;

use App\Models\Objects\FieldType;
use App\Models\Pages\Page;
use App\Models\Publications\Publication;
use App\Services\Forms\FormsService;
use App\Transformers\Assets\AssetTransformer;
use App\Transformers\Publications\PublicationTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

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

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

    public function commonGroupFields(?Publication $model): array {
        $fields = [
            [
                'name' => 'name',
                'title' => 'Название',
                'type' => FieldType::STRING,
                'required' => true,
                'max_length' => 750,
                'value' => $model->name ?? null
            ],
            [
                'name' => 'excerpt',
                'title' => 'Краткое содержание',
                'type' => FieldType::TEXT,
                'required' => true,
                'value' => $model->excerpt ?? null
            ],
            [
                'name' => 'is_published',
                'title' => 'Опубликовано',
                'type' => FieldType::BOOLEAN,
                'value' => $model->is_published ?? null
            ],
            [
                'name' => 'published_at',
                'title' => 'Дата публикации',
                'type' => FieldType::DATE,
                'value' => $model->published_at ?? null,
                'required' => true,
            ],
            [
                'name' => 'images',
                'title' => 'Изображения',
                'type' => FieldType::IMAGE,
                'multiple' => true,
                'value' => ($v = $model->images ?? null) ? fractal($v, new AssetTransformer()) : null
            ],
            [
                'name' => 'is_blank',
                'title' => 'Скрыть постер и анонс на странице публикации',
                'type' => FieldType::BOOLEAN,
                'value' => $model->is_blank ?? null
            ],
            [
                'name' => 'content',
                'title' => 'Содержимое публикации',
                'type' => FieldType::HTML,
                'value' => $model->content ?? null
            ]
        ];
        return ['data' => $fields];
    }


    public function store(array $data): ?JsonResponse {
        $page = Page::byUuid($data['page'] ?? null)->firstOrFail();
        $data = array_merge($data, ['author_id' => Auth::user()->id, 'slug' => Str::slug($data['slug'] ?? $data['name']), 'is_published' => !empty($data['is_published']), 'is_blank' => !empty($data['is_blank'])]);
        $model = $page->publications()->create($data);
        $model->setImages($data['images'] ?? null);
        return fractal($model, new PublicationTransformer())->respond();
    }

    public function update(string $id, array $data): ?JsonResponse {
        $model = Publication::byUuid($id)->firstOrFail();
        $data = array_merge($data, ['slug' => Str::slug($data['slug'] ?? $data['name']), 'is_published' => !empty($data['is_published']), 'is_blank' => !empty($data['is_blank'])]);
        $model->update($data);
        $model->setImages($data['images'] ?? null);
        return fractal($model->fresh(), new PublicationTransformer())->respond();
    }
}
