<?php

namespace Model;
use Gateway\FavoriteGateway;
use Entity\FavoriteEntity;

class FavoriteModel
{
    private FavoriteGateway $gw;

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

    public function createFavoriteModel(int $idUser, int $idQuote) : bool
    {
        return $this -> gw -> createFavoriteGateway($idUser, $idQuote);
    }

    public function getFavoriteById(int $idUser, int $idQuote) : ?FavoriteEntity
    {
        $res = $this -> gw -> findFavoriteById($idUser, $idQuote);

        if ($res)
        {
            return new FavoriteEntity (
                $res[0]['user_f'],
                $res[0]['quote_f'],
            );
        }
        return null;
    }

    public function getFavoriteByUser(int $idUser) : array
    {
        $res = $this -> gw -> findFavoriteByUser($idUser);

        $favorites = [];

        foreach ($res as $favorite)
        {
            $favorites = new FavoriteEntity (
                $favorite['user_f'],
                $favorite['quote_f']
            );
        }
        return $favorites;
    }

    public function getAllFavorite() : array
    {
        $res = $this -> gw -> findAllFavorite();

        $favorites = [];

        foreach ($res as $favorite)
        {
            $favorites = new FavoriteEntity (
                $favorite['user_f'],
                $favorite['quote_f']
            );
        }
        return $favorites;
    }

    public function removeFavorite(int $idUser, int $idQuote) : bool
    {
        return $this -> gw -> deleteFavoriteGateway($idUser, $idQuote);
    }
}