<?php

declare(strict_types=1);

namespace App\Http\Requests\Collections;

use App\Models\Collection;
use Illuminate\Foundation\Http\FormRequest;

class UpdateCollectionPresentationRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    protected function prepareForValidation(): void
    {
        foreach (['subtitle', 'summary', 'theme_token'] as $field) {
            if ($this->has($field) && trim((string) $this->input($field)) === '') {
                $this->merge([$field => null]);
            }
        }
    }

    public function rules(): array
    {
        return [
            'subtitle' => ['nullable', 'string', 'max:160'],
            'summary' => ['nullable', 'string', 'max:320'],
            'presentation_style' => ['nullable', 'in:' . implode(',', [
                Collection::PRESENTATION_STANDARD,
                Collection::PRESENTATION_EDITORIAL_GRID,
                Collection::PRESENTATION_HERO_GRID,
                Collection::PRESENTATION_MASONRY,
            ])],
            'emphasis_mode' => ['nullable', 'in:' . implode(',', [
                Collection::EMPHASIS_COVER_HEAVY,
                Collection::EMPHASIS_BALANCED,
                Collection::EMPHASIS_ARTWORK_FIRST,
            ])],
            'theme_token' => ['nullable', 'in:default,subtle-blue,violet,amber'],
            'layout_modules_json' => ['nullable', 'array'],
            'layout_modules_json.*.key' => ['required_with:layout_modules_json', 'string', 'max:60'],
            'layout_modules_json.*.enabled' => ['nullable', 'boolean'],
            'layout_modules_json.*.slot' => ['nullable', 'string', 'max:20'],
        ];
    }
}