<?php

namespace App\Http\Requests\Admin\Manage;

use App\Traits\NotificationTrait;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

class InventoryRequest extends FormRequest
{
    use NotificationTrait;

    public function authorize(): bool
    {
        return Auth::check();
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'price' => removeRupiahFormatting($this->price),
        ]);
    }

    public function rules(): array
    {
        $isEdit = $this->isMethod('put');

        return [
            'name' => ['required', 'string', 'max:100'],
            'quantity' => ['required', 'numeric', 'min:0'],
            'price' => ['required', 'numeric', 'min:0', 'max:99999999.99'],
            'image' => $isEdit
                ? ['nullable', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048']
                : ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'],
            'description' => ['required', 'string', 'max:65535'],
        ];
    }

    protected function failedValidation(Validator $validator): void
    {
        $this->handleValidationFailure($validator);
    }
}
