<?php

namespace App\Model;

use Nette;

class PersonManager
{
	use Nette\SmartObject;

	private Nette\Database\Explorer $database;

	public function __construct(Nette\Database\Explorer $database)
	{
		$this->database = $database;
	}

	public function getById($userId)
    {
        return $this->database->table('osoby')->get($userId);
    }

    public function getByEmail($email)
    {
        return $this->database->table('osoby')->where('email', $email);
    }

    public function getAllInstructors()
    {
        return $this->database->table('osoby');
    }

	public function changeRole($id, $newRole)
    {
		return $this->database->table('osoby')->where('id', $id)->update([
			'opravneni' => $newRole
		]);
    }

	public function updateProfile($id, $newData)
    {
        return $this->database->table('osoby')->where('id', $id)->update($newData);
    }

	
	public function registerInstructor($newUser)
    {
        try
        {
            $newUser['heslo'] = password_hash($newUser['heslo'], PASSWORD_HASH_ALGO, PASSWORD_HASH_OPTIONS);
            $result = $this->database->table('osoby')->insert($newUser);
        }
        catch (Nette\Database\UniqueConstraintViolationException $e)
        {
            return false;
        }
        return $result;
    }

}