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

class CommentaryModel {

    private commentaryGateway $gw;

    function __construct(commentaryGateway $gw) {
        $this->gw = $gw;
    }

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

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

    public function getComments(): array {
        $res = $this->gw->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 -> gw -> delete($id_comment);
    }

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

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

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

}
