<?php

namespace App\Model;

use Nette;

class CourseManager
{
	use Nette\SmartObject;

	private Nette\Database\Explorer $database;

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

	public function createCourse($courseData)
	{
		return $this->database->table('kurzy')->insert($courseData);
	}

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

	public function createQuestionnaire($questionnaireData)
	{
		unset($questionnaireData['cas_vyplneni'], $questionnaireData['cas_zmeny']);
		return $this->database->table('dotazniky')->insert($questionnaireData);
	}

	public function updateQuestionnaire($questionnaireData)
	{
		$id = $questionnaireData['ID_kurzu'];
		unset($questionnaireData['cas_vyplneni']);
		//$questionnaireData['cas_prijezdu'] = $questionnaireData['cas_prijezdu']->h . ':' . $questionnaireData['cas_prijezdu']->i . ':' . $questionnaireData['cas_prijezdu']->s;
		//$questionnaireData['cas_odjezdu'] = $questionnaireData['cas_odjezdu']->h . ':' . $questionnaireData['cas_odjezdu']->i . ':' . $questionnaireData['cas_odjezdu']->s;
		$questionnaireData['cas_zmeny'] = date('Y-m-d H:i:s');
		return $this->database->table('dotazniky')->where('ID_kurzu', $id)->update($questionnaireData);
	}

	public function createInstructorEvaluation($evaluationData)
	{
		return $this->database->table('hodnoceni')->insert($evaluationData);
	}

	public function updateInstructorEvaluation($evaluationData)
	{
		$courseId = $evaluationData['ID_kurzu'];
		$personId = $evaluationData['ID_hodnoceneho'];
		unset($evaluationData['ID']);
		return $this->database->table('hodnoceni')->where('ID_kurzu = ? AND ID_hodnoceneho = ?', $courseId, $personId)->update($evaluationData);
	}

	public function updateCourseReport($courseId, $reportText)
	{
		return $this->database->table('kurzy')->where('ID', $courseId)->update([ 'zapis' => $reportText ]);
	}

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

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

	public function getCoursesByTimespan($from, $to)
	{
		return $this->database->query("SELECT * FROM `kurzy` WHERE datum_zacatku >= ? AND datum_zacatku <= ?", $from, $to);
	}

	public function getCoursesByInstructor($userId)
	{
		return $this->database->query("SELECT kurzy.* FROM `kurzy`
											JOIN osoba_kurz ON kurzy.ID = osoba_kurz.ID_kurzu
											WHERE osoba_kurz.ID_osoby = ?", $userId);
	}

	public function getCoursesBySchool($schoolId)
	{
		return $this->database->query("SELECT * FROM `kurzy` WHERE ID_skoly = ?", $schoolId);
	}

	public function getCoursesByState($state)
	{
		return $this->database->query("SELECT * FROM `kurzy` WHERE stav = ?", $state);
	}

	public function getCourseInstructors($id)
	{
		return $this->database->table('osoba_kurz')->where('ID_kurzu',$id);
	}

	public function getCourseQuestionnaire($id)
	{
		$result = $this->database->table('dotazniky')->where('ID_kurzu',$id);
		if($result->count('*') == 0) {
			return null;
		}
		return $result->fetch();
	}

	public function updateCourseInstructors($courseId, $roleId, $data)
	{
		$this->database->table('osoba_kurz')->where('ID_kurzu = ? AND cislo_role = ?', $courseId, $roleId)->delete();// smazu vsechny predchozi instruktory s touto roli

		// odstranim pripadne instruktory v jinych rolich
		$instructorIds = array();
		foreach ($data as $row) {
			$instructorIds[] = $row['ID_osoby'];
		}
		if(count($instructorIds) > 0) {
			$this->database->table('osoba_kurz')->where('ID_kurzu = ? AND ID_osoby IN ?', $courseId, $instructorIds)->delete();// smazu vsechny predchozi instruktory s touto roli
		}
		
		$insertData = $data;
		if(count($data) == 0) {
			return true;
		}
		foreach ($data as $key => $row) {
			$insertData[$key]['ID_kurzu'] = $courseId;
		}
		return $this->database->table('osoba_kurz')->insert($insertData);
	}

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

	public function removeCourseInstructor($courseId, $instructorId)
	{
		return $this->database->table('osoba_kurz')->where('ID_kurzu = ? AND ID_osoby = ?', $courseId, $instructorId)->delete();
	}
}