<?php

namespace Modules\Admin\app\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class BannerRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $actionMethod = $this->route()->getActionMethod();

        $rules = [
            'title' => 'max:255',
            'link' => 'nullable|max:255',
            'image' => 'file|mimes:jpeg,jpg,png,gif|max:2048',
            'is_active' => 'in:true,false',
        ];

        // single
        if ($actionMethod === 'create') {
            $rules['title'] = 'required|' . $rules['title'];
        }
        if ($actionMethod === 'update') {
            $rules['id'] = 'required|exists:banner,id';
            $rules['title'] = 'required|' . $rules['title'];
        }
        if ($actionMethod === 'delete') {
            $rules['id'] = 'required|exists:banner,id';
        }

        // multiple
        if ($actionMethod === 'creates') {
            $rules = [
                'banners.*.id' => 'required|exists:banner,id',
                'banners.*.title' => $rules['title'],
                'banners.*.link' => $rules['link'],
                'banners.*.image' => $rules['image'],
                'banners.*.is_active' => $rules['is_active'],
            ];
        }
        if ($actionMethod === 'updates') {
            $rules = [
                'banners.*.title' => $rules['title'],
                'banners.*.link' => $rules['link'],
                'banners.*.image' => $rules['image'],
                'banners.*.is_active' => $rules['is_active'],
            ];
        }
        if ($actionMethod === 'deletes') {
            $rules = [
                'banners.*.id' => 'required|exists:banner,id'
            ];
        }

        return $rules;
    }

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