<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CropSizeRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        if (app()->environment('local')) {
            return true;
        }
        $heightValue = $this->input('height') ?? 'Auto';
        $encryption_string = config('media.crop_hash') . ':' . $this->mediaUse->id
            . ':' . $this->input('width') . ':' . $heightValue;
        return $this->mediaUse
            && $this->input('token')
            && $this->input('token') === md5($encryption_string);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'tenant' => 'required',
            'token' => 'required',
            'width' => [
                'required',
                'numeric',
                'min:1',
                'max:4000',
            ],
            'height' => [
                'nullable',
                'numeric',
                'min:1',
                'max:4000',
            ],
        ];
    }
}
