<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class UniqueCustom implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */

    private $column = "";
    private $table = "";
    public function __construct($table, $column)
    {
        //
        $this->column = $column;
        $this->table = $table;

    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        //

        $result = DB::table($this->table)->where($this->column, basicDecrypt($value))->exists();
        if ($result) {
            return true;
        }
        return $this->fail();

    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute is not a valid value.';
    }
    /**
     * @param $message
     * @return false
     */
    protected function fail()
    {
        $message = Auth::user()->email . ' try to pass invalid id on ' . $this->table . ' table.';
        saveLog($message, Auth::user(), [], 'security breach');

        return false;
    }
}
