<?php

namespace App\Http\Requests\Api\Gallery;

use Illuminate\Foundation\Http\FormRequest;
use App\Support\Formatter;
use App\Models\Gallery\GallerySection;

class GallerySectionUpdateRequest extends FormRequest
{
    public function __construct(Formatter $formatter)
    {
        $this->formatter = $formatter;
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        $user = auth()->user();

        return $user->mainAccount->uid === $this->route('accountUid');
    }

    /**
     * Prepare the data for validation.
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->merge([
            'name' => $this->formatter->transformItem($this->name, 'strip_tags|xss_clean|trim'),
            'description' => $this->formatter->transformItem($this->description, 'strip_tags|xss_clean|trim'),
        ]);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $layoutTypes = implode(',', GallerySection::LAYOUT_TYPES);
        $layoutWidths = implode(',', GallerySection::LAYOUT_WIDTHS);
        $imageSizes = implode(',', GallerySection::IMAGE_SIZES);
        $imageSpacings = implode(',', GallerySection::IMAGE_SPACINGS);

        return [
            'name' => 'nullable|max:255',
            'description' => 'nullable|max:5000',
            'settings' => 'nullable|array',
            'settings.layout' => "required_with:settings|in:$layoutTypes",
            'settings.layoutOptions' => "required_with:settings|array",
            'settings.layoutOptions.width' => "required_with:settings|in:$layoutWidths",
            'settings.layoutOptions.imageSize' => "required_with:settings|in:$imageSizes",
            'settings.layoutOptions.imageSpacing' => "required_with:settings|in:$imageSpacings",
        ];
    }
}
