<?php

namespace App\Http\Requests\Api;

use App\Libs\Security\XSSCleanFilter;
use Illuminate\Foundation\Http\FormRequest;
use App\Libs\Support\Sanitizer\Traits\SanitizesInput;

class MediaDownloadRequest extends FormRequest
{
    use SanitizesInput;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'slug' => 'bail|required|string|max:255|regex:/^[a-zA-Z0-9_\-\.]+$/',
        ];
    }

    /**
     * Get the sanitization filters that apply to the request.
     *
     * @return array
     */
    public function filters()
    {
        return [
            'slug' => "trim|strip_tags|xss_clean",
        ];
    }

    /**
     * Get the route parameters and merge them with the request data.
     *
     * @param array|null $keys
     * @return array
     */
    public function all($keys = null)
    {
        $data = parent::all();

        if ($this->route()->hasParameters()) {
            $parameterNames = $this->route()->parameterNames();

            if (!empty($parameterNames)) {
                $parameterName = $parameterNames[0];
                $data['slug'] = $this->route()->parameter($parameterName);
            }
        }

        return $data;
    }

    /**
     * Get custom filter classes.
     *
     * @return array
     */
    public function customFilters()
    {
        return ['xss_clean' => XSSCleanFilter::class];
    }

    /**
     * Get custom error messages.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'slug.regex' => 'Invalid media filename format.',
        ];
    }
}
