<?php

namespace App\Model;

use Nette;

class SchoolManager
{
	use Nette\SmartObject;

	private Nette\Database\Explorer $database;

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

	public function getAllSchools()
	{
		return $this->database->table('skoly');
	}

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

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

	public function create($data)
	{
		return $this->database->table('skoly')->insert($data);
	}

	public function update($data)
	{
		$id = $data['ID'];
		return $this->database->table('skoly')->where('id', $id)->update($data);
	}

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