<?php

namespace App\Http\Requests\Dashboard;

use Illuminate\Foundation\Http\FormRequest;
use Propaganistas\LaravelPhone\PhoneNumber;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            "name" => "required|min:2|max:255",
            "surname" => "required|min:2|max:255",
            "mobile_number_country" => "required_with:mobile_number|string|size:2",
            "mobile_number" => "phone:mobile_number_country|exists:users,mobile_number",
            "profile_link" => "string|min:5|max:15|unique:user_profiles,profile_link,". auth()->user()->userProfile->id,
            "short_bio" => "max:100",
        ];
    }

    public function getValidatorInstance()
    {
        if($this->request->has("mobile_number") && $this->request->has("mobile_number_country")) {
            $this->request->set("mobile_number", PhoneNumber::make(
                $this->request->get("mobile_number"),
                $this->request->get("mobile_number_country")
            )->formatInternational());
        }

        return parent::getValidatorInstance();
    }
}
