<?php

namespace App\Core\Validator\User;

use App\Core\Repository\User\UserRepository;
use App\Models\User;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Support\Facades\Hash;

use App\Core\Validator\BaseValidator;

use Exception;

class ChangePasswordValidator extends BaseValidator
{
    private $userRepository;

    public function __construct
    (
        UserRepository $userRepository,
        Translator $translator,
        Container $container
    )
    {
        parent::__construct($translator, $container);
        $this->userRepository = $userRepository;
    }

    public function rules($params = null)
    {
        return
            [
                "user_id" => "required",
                "checkOldPassword" => "accepted",
                'password' => 'required|max:20|min:6|confirmed',
            ];


    }

    public function messages()
    {
        $thisMessages = [];
        return array_merge(parent::messages() , $thisMessages);
    }

    public function checkOldPassword($params)
    {
        try {


            $user = User::where('id', $params['user_id'])->first();
            $result = Hash::check($params['old_password'], $user->password) ;
            return $result ? true : false;

        } catch (Exception $e) {
            return false;
        }

    }

    public function validate(array $params, array $rules = [], array $messages = [], array $customAttributes = [])
    {
        $params["checkOldPassword"] = $this->checkOldPassword(['user_id' => $params["user_id"], 'old_password' => $params["old_password"] ]);
        return $this->make($params, $this->rules($params), $this->messages());
    }

}
