<?php

namespace App\Http\Requests\Admin\Master;

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

class BarbershopRequest extends FormRequest
{
    use NotificationTrait;

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

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

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

        return [
            'name' => ['required', 'string', 'max:50'],
            'contact_number' => ['required', 'string', 'regex:/^\+?\d{10,15}$/'],
            'landmark' => ['required', 'string', 'max:30'],
            'address' => ['required', 'string', 'max:65535'],
            'opened' => ['required', 'date'],
            'closed' => ['nullable', 'date'],
            'cash' => ['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'],
        ];
    }

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