<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use Illuminate\Support\Str;
use Illuminate\Validation\Rule;

use App\Models\ContentElement;

class ContentElementValidation extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if (!auth()->check()) {
            return false;
        }

        if (request('type') === 'embed-code') {
            return auth()->user()->hasRole('admin');
        }

        $contentable = ContentElement::findContentable(requestInput());
        return auth()->user()->can('update', $contentable);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        if (!requestInput('type')) {
            return [
                'type' => 'required',
            ];
        }

        $content_class = '\App\\Http\\Requests\\'.Str::studly(requestInput('type')).'Validation';

        $rules = collect([
            'content' => 'required|array',
            'pivot.contentable_id' => 'required',
            'pivot.contentable_type' => 'required',
            'pivot.sort_order' => 'required|numeric',
            'pivot.unlisted' => 'required|boolean',
            'pivot.expandable' => [
                'nullable',
                'string',
                Rule::in(['expand', 'modal']),
            ],
            'pivot.guest' => 'required|boolean',
            'pivot.no_margin' => 'required|boolean',
            'pivot.randomize' => 'required|boolean',
            'pivot.filter' => 'required|boolean',
            'pivot.hide_print' => 'required|boolean',
        ]);

        $rules = $rules->merge(
            collect((new $content_class())->rules())->mapWithKeys(function ($rule, $field) {
                return ['content.'.$field => $rule];
            })
        );

        return $rules->all();
    }
}
