<?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;
use Illuminate\Validation\Rule;

class TransactionRequest extends FormRequest
{
    use NotificationTrait;

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

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

    public function rules(): array
    {
        return [
            'barber' => ['required', Rule::exists('users', 'id')],
            'service' => ['required', Rule::exists('services', 'id')],
            'customer' => ['required'],
            'price' => ['required', 'numeric', 'min:0', 'max:999999.99'],
            'discount' => ['required', 'numeric', 'min:0', 'max:999999.99'],
            'total' => ['required', 'numeric', 'min:0', 'max:999999.99'],
            'image' => ['nullable', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'],
            'payment_method' => ['required', Rule::in(['1', '2', '3'])],
        ];
    }

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