<?php

namespace App\Services\Forms\Publications;

use App\Models\Asset;
use App\Models\Objects\FieldType;
use App\Models\Pages\Page;
use App\Models\Publications\Publication;
use App\Models\Publications\PublicationType;
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 PublicationNewsForms 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' => 'published_at',
                'title' => 'Дата публикации',
                'type' => FieldType::DATE,
                'value' => $model->published_at ?? null,
                'required' => true,
            ],
            [
                'name' => 'name',
                'title' => 'Название',
                'type' => FieldType::STRING,
                'required' => true,
                'max_length' => 127,
                'value' => $model->name ?? null
            ],
            [
                'name' => 'excerpt',
                'title' => 'Краткое описание',
                'type' => FieldType::TEXT,
                'required' => true,
                'value' => $model->excerpt ?? null
            ],
            [
                'name' => 'poster_id',
                'title' => 'Превью',
                'type' => FieldType::IMAGE,
                'required' => true,
                'value' => ($model->poster ?? null) ? fractal($model->poster, new AssetTransformer()) : null
            ]
        ];
        return ['data' => $fields];
    }


    public function store(array $data): ?JsonResponse {
        if (!empty($data['attach']['page_id'])) {
            $page = Page::byUuid($data['attach']['page_id'])->first();
            $data['page_id'] = $page->id;
        }
        $data['author_id'] = Auth::user()->id;
        $data['slug'] = Str::slug(Str::transliterate($data['name']));
        $data['type'] = PublicationType::NEWS;
        if (!empty($data['poster_id'])) {
            $asset = Asset::query()->where(['uuid' => $data['poster_id']])->first();
            $data['poster_id'] = $asset->id;
        }
        unset($data['attach']);
        $model = Publication::create($data, true);
        return fractal($model, new PublicationTransformer())->respond();
    }

    public function update(string $id, array $data): ?JsonResponse {
        $model = Publication::byUuid($id)->firstOrFail();
        $data['author_id'] = Auth::user()->id;
        if (!empty($data['poster_id'])) {
            $asset = Asset::query()->where(['uuid' => $data['poster_id']])->first();
            $data['poster_id'] = $asset->id;
        }
        $model->update($data);
        return fractal($model->fresh(), new PublicationTransformer())->respond();
    }
}
