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

class CommentaryModel {

    private commentaryGateway $gw;

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

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

        $c = new CommentaryEntity($id_comment, $comment, $date);
        return $this->gw->create($c);
    }

    public function getComment(int $id_comment): ?commentaryEntity {
        $res = $this->gw->findById($id_comment);
        if($res){
            return new CommentaryEntity($res[0]["id_comment"], $res[0]["comment"], $res[0]["date"]);
        }
    }

    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;
    }

}
