<?php

namespace Model;

use Entity\ResultsEntity;
use Gateway\ResultsGateway;
use Gateway\Gateway;

class ResultsModel extends Model
{
    
    public function createResultsModel(int $idQuiz, int $idUser, int $score, int $time) : bool
    {
        return $this -> gateway -> createResultsGateway($idQuiz, $idUser, $score, $time);
    }

    public function getResultsByQuiz(int $idQuiz) : array
    {
        $res = $this -> gateway -> findResultsByQuiz($idQuiz);

        $results = [];

        foreach ($res as $result)
        {
            $results[] = new ResultsEntity (
                $result['user_r'],
                $result['quiz_r'],
                $result['score'],
                $result['time']
            );
        }
        return $results;
    }

    public function getResultsByUser(int $idUser) : array
    {
        $res = $this -> gateway -> findResultsByUser($idUser);

        $results = [];

        foreach ($res as $result)
        {
            $results[] = new ResultsEntity (
                $result['user_r'],
                $result['quiz_r'],
                $result['score'],
                $result['time']
            );
        }
        return $results;
    }

    public function getResultsById(int $idQuiz, int $idUser) : ?ResultsEntity
    {
        $res = $this -> gateway -> findResultsById($idQuiz, $idUser);

        if ($res)
        {
            return new ResultsEntity (
                $res['user_r'],
                $res['quiz_r'],
                $res['score'],
                $res['time']
            );
        }
        return null;
    }

    public function getAllResults() : array
    {
        $res = $this -> gateway -> findAllResults();

        $results = [];

        foreach ($res as $result)
        {
            $results[] = new ResultsEntity (
                $result['user_r'],
                $result['quiz_r'],
                $result['score'],
                $result['time']
            );
        }
        return $results;
    }

    public function updateResultsModel(int $idQuiz, int $idUser, ?int $score, ?int $time) : bool
    {
        return $this -> gateway -> updateResults($idQuiz, $idUser, $score, $time);
    }
}