<?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 PublicationPhotosForms
    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 {

        $params = $model->parsedParams ?? null;

        if (empty($params->assets)) {
            $assets = [];
        } else {
            $models = Asset::query()->whereIn('uuid', $params->assets)->orderBy('id')->get();
            $assets = fractal($models, new AssetTransformer());
        }

        $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' => 'assets',
                'title' => 'Фотографии',
                'type' => FieldType::IMAGE,
                'multiple' => true,
                'required' => true,
                'value' => $assets
            ],
        ];
        return ['data' => $fields];
    }



    public function store(array $data): ?JsonResponse {
        $page = Page::byUuid($data['attach']['page_id'])->first();
        $pub = [];
        $pub['page_id'] = $page->id;
        $pub['author_id'] = Auth::user()->id;
        $pub['type'] = PublicationType::PHOTOS;
        $pub['name'] = $data['name'];
        $pub['published_at'] = $data['published_at'];
        if (!empty($data['assets'])) {
            $pub['params'] = json_encode([
                'assets' => $data['assets']
            ]);
        }

        $model = Publication::create($pub, true);
        return fractal($model, new PublicationTransformer())->respond();
    }

    public function update(string $id, array $data): ?JsonResponse {
        $model = Publication::byUuid($id)->firstOrFail();
        $pub = [];
        if (!empty($data['name'])) {
            $pub['name'] = $data['name'];
        }
        if (!empty($data['assets'])) {
            $pub['params'] = json_encode([
                'assets' => $data['assets']
            ]);
        }
        $pub['published_at'] = $data['published_at'];

        $model->update($pub);
        return fractal($model, new PublicationTransformer())->respond();
    }
}
