<?php
namespace Model;
use Entity\CommentaryEntity;
use Gateway\CommentaryGateway;
use Gateway\Gateway;

class CommentaryModel extends Model {

    public function createComment(string $comment, string $idQuote, string $idUser): bool {
        return $this->gateway->create($comment, $idUser, $idQuote);
    }

    public function getComment(int $id): array {
        $com = [];
        $res = $this->gateway->findByQuote($id);
        foreach ($res as $comments){
            $com[] = new CommentaryEntity($comments["id_comment"], $comments["comment"], $comments["datec"], $comments["username"], $comments["imgpath"]);
        }
        return $com;
    }

    public function getComments(): array {
        $res = $this->gateway->findAll();
        foreach ($res as $comments) {
            $com[] = new CommentaryEntity($comments["id_comment"], $comments["comment"], $comments["date"]);
        }
        return $com;
    }

    public function deleteComment(int $id_comment): bool {
        return $this -> gateway -> delete($id_comment);
    }

    public function updateComment(int $id_comment, string $comment): bool {

        $c = $this -> getComment($id_comment);

        if($c){
            $c -> setComment($comment);
            return $this->gateway -> update($c);
        }
        return false;
    }

}
