<?php

namespace Azine\EmailBundle\Entity\Repositories;

use Doctrine\ORM\EntityRepository;

/**
 * NotificationRepository.
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class NotificationRepository extends EntityRepository
{
    /**
     * Get all notifications that should be sent.
     *
     * @param $recipientId
     *
     * @return array of Notification
     */
    public function getNotificationsToSend($recipientId)
    {
        $qb = $this->createQueryBuilder('n')
            ->andWhere('n.sent is null')
            ->andWhere('n.recipient_id = :recipientId')
            ->setParameter('recipientId', $recipientId)
            ->orderBy('n.importance', 'desc')
            ->orderBy('n.template', 'asc')
            ->orderBy('n.title', 'asc');
        $notifications = $qb->getQuery()->execute();

        return $notifications;
    }

    /**
     * Get all notifications that should be sent immediately.
     *
     * @param $recipientId
     *
     * @return array of Notification
     */
    public function getNotificationsToSendImmediately($recipientId)
    {
        $qb = $this->createQueryBuilder('n')
            ->andWhere('n.sent is null')
            ->andWhere('n.send_immediately = true')
            ->andWhere('n.recipient_id = :recipientId')
            ->setParameter('recipientId', $recipientId)
            ->orderBy('n.importance', 'desc')
            ->orderBy('n.template', 'asc')
            ->orderBy('n.title', 'asc');
        $notifications = $qb->getQuery()->execute();

        return $notifications;
    }

    /**
     * Get all recipients with unsent Notifications.
     *
     * @return array
     */
    public function getNotificationRecipientIds()
    {
        $qb = $this->getEntityManager()->createQueryBuilder()
            ->select('n.recipient_id')
            ->distinct()
            ->from("Azine\EmailBundle\Entity\Notification", 'n')
            ->andWhere('n.sent is null');
        $results = $qb->getQuery()->execute();

        $ids = array();
        foreach ($results as $next) {
            $ids[] = $next['recipient_id'];
        }

        return $ids;
    }

    /**
     * Mark all notifications as sent "far in the past". This is used for users that don't want to receive any notifications.
     *
     * @param $recipientId
     */
    public function markAllNotificationsAsSentFarInThePast($recipientId)
    {
        $qb = $this->getEntityManager()->createQueryBuilder()
            ->update("Azine\EmailBundle\Entity\Notification", 'n')
            ->set('n.sent', ':farInThePast')
            ->andWhere('n.sent is null')
            ->andWhere('n.recipient_id = :recipientId')
            ->setParameter('recipientId', $recipientId)
            ->setParameter('farInThePast', new \DateTime('1900-01-01'));
        $qb->getQuery()->execute();
    }

    /**
     * Get the \DateTime of the last Notification that has been sent.
     *
     * @param $recipientId
     *
     * @return \DateTime
     */
    public function getLastNotificationDate($recipientId)
    {
        $qb = $this->getEntityManager()->createQueryBuilder()
            ->select('max(n.sent)')
            ->from("Azine\EmailBundle\Entity\Notification", 'n')
            ->andWhere('n.recipient_id = :recipientId')
            ->setParameter('recipientId', $recipientId);
        $results = $qb->getQuery()->execute();
        if (null === $results[0][1]) {
            // the user has not received any notifications yet ever
            $lastNotification = new \DateTime('@0');
        } else {
            $lastNotification = new \DateTime($results[0][1]);
        }

        return $lastNotification;
    }
}
