<?php

namespace App\Http\Requests\Api\Dashboard\Admin\Workout;

use App\Http\Requests\Api\ApiMasterRequest;
use Illuminate\Validation\Rule;

class WorkoutRequest extends ApiMasterRequest
{
    /**
     * 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<string, mixed>
     */
    public function rules()
    {
        $required = $this->isMethod('post') ? 'required' : 'nullable';
        $rules = [
            'workout_category_id'    => ['required', 'exists:workout_categories,id'],
            'video'                  => [$required, 'string', 'max:255'],
            'image'                  => [$required, 'string', 'max:255'],
        ];
        foreach (config('translatable.locales') as $locale) {
            $rules[$locale . '.title']        = [
                'required',
                'between:3,250',
                Rule::unique('workout_translations', 'title')->where(function ($query) use ($locale) {
                    return $query->where('locale', $locale)->where('workout_id', '!=', $this->workout);
                })
            ];
            $rules[$locale . '.desc']        = [
                'required',
                'between:3,1000',
            ];
        }
        return $rules;
    }
}
