<?php

namespace Src\Modules\Auth\Application\Admin;

use Src\Common\Application\TraitValidator;
use Src\Modules\Auth\Interfaces\Application\Admin\IValidator;
use Illuminate\Validation\Rule;

class Validator implements IValidator
{
    use TraitValidator;

    protected string $adminsTable;

    public function setAdminsTable(string $table): void
    {
        $this->adminsTable = $table;
    }

    public function login(array $data):bool
    {
        $rules = [
            'email' => [
                'required',
                'email',
            ],
            'password' => [
                'required',
            ],
        ];
        $messages = [
            'required' => 'Поле обязательно для заполнения',
            'email' => 'Не корректный емайл',
        ];
        return $this->validate($data, $rules, $messages);
    }

    public function insert(array $data): bool
    {
        $rules = [
            'email' => [
                'required',
                'email',
                Rule::unique($this->adminsTable, 'email'),
            ],
            'password' => [
                'required',
                'max:256',
            ],
        ];
        $messages = [
            'required' => 'Обязательное поле',
            'email' => 'Некорректный Email',
        ];
        return $this->validate($data, $rules, $messages);
    }

    public function update(array $data): bool
    {
        $rules = [
            'id' => [
                'nullable',
                'max:256',
            ],
            'email' => [
                'required',
                'email',
                Rule::unique($this->adminsTable, 'email')->ignore($data['id'], 'id'),
            ],
            'password' => [
                'nullable',
                'max:256',
            ],
        ];
        $messages = [
            'required' => 'Обязательное поле',
            'email' => 'Некорректный Email',
        ];
        return $this->validate($data, $rules, $messages);
    }

    
}