<?php

namespace App\Http\Repositories;

use App\Models\AuthVerificationToken;

class AuthVerificationTokenRepository
{
    public function create($identifier, $identifier_type, $code)
    {
        AuthVerificationToken::where("identifier", $identifier)->where("identifier_type", $identifier_type)->delete();

        AuthVerificationToken::insert([
            'identifier' => $identifier,
            'identifier_type' => $identifier_type,
            'token' => hash('sha512', $code),
            'created_at' => now()
        ]);
    }

    public function verify($identifier, $identifier_type, $code)
    {
        $password_recovery = AuthVerificationToken::where("identifier", $identifier)
            ->where("identifier_type", $identifier_type)
            ->where("token", hash('sha512', $code))
            ->first();

        if (!$password_recovery)
            return false;

        return true;
    }

    public function reset($identifier, $identifier_type, $code)
    {
        if (!$this->verify($identifier, $identifier_type, $code))
            return false;

        return AuthVerificationToken::where("identifier", $identifier)->where("identifier_type", $identifier_type)->delete();
    }
}
