<?php

namespace ArcaSolutions\WebBundle\Repository;

use ArcaSolutions\WebBundle\Entity\Accountprofilecontact;
use ArcaSolutions\WebBundle\Entity\Review;
use ArcaSolutions\WebBundle\Entity\Setting;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Form;

/**
 * FaqRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ReviewRepository extends EntityRepository
{
    /**
     * @param int $id
     * @param string|listing|classified|event $module
     *
     * @return array
     */
    public function getTotalByItemId($id, $module)
    {
        return $this->createQueryBuilder('r')
            ->select('count(r.id)')
            ->where('r.itemType = :module')
            ->andWhere('r.approved = :approved')
            ->andWhere('r.itemId = :id')
            ->setParameter('module', $module)
            ->setParameter('approved', 1)
            ->setParameter('id', $id)
            ->getQuery()
            ->getOneOrNullResult();
    }

    /**
     * @param int $id
     * @param string|listing|classified|event $module
     *
     * @return mixed
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function getOneGoodReview($id, $module)
    {
        return $this->createQueryBuilder('r')
            ->where('r.itemType = :module')
            ->andWhere('r.approved = :approved')
            ->andWhere('r.itemId = :id')
            ->andWhere('r.review <> :empty')
            ->setParameter('module', $module)
            ->setParameter('approved', 1)
            ->setParameter('empty', '')
            ->setParameter('id', $id)
            ->orderBy('r.rating', 'DESC')
            ->setMaxResults(1)
            ->getQuery()
            ->getOneOrNullResult();
    }

    /**
     * @param string $module [Listing, Article]
     * @param integer $item
     * @param Accountprofilecontact $member
     * @param Form $form
     *
     * @return Review
     * @throws \Exception
     */
    public function addReview($module, $item, Accountprofilecontact $member = null, Form $form)
    {
        $review = new Review();

        /* Gets the configuration of the review it is approved or not */
        $approvedSetting = $this->getEntityManager()->getRepository('WebBundle:Setting')
            ->getSetting('review_approve');
        $approved = $approvedSetting ? 0 : 1;

        $review->setItemType($module)
            ->setItemId($item)
            ->setAdded(new \DateTime())
            ->setIp($_SERVER['REMOTE_ADDR'])
            ->setReviewerName($form->get('name')->getData())
            ->setReviewTitle($form->get('title')->getData())
            ->setReviewerEmail($form->get('email')->getData())
            ->setReviewerLocation($form->get('location')->getData())
            ->setReview($form->get('message')->getData())
            ->setRating($form->get('rating')->getData())
            ->setLike(0)
            ->setDislike(0)
            ->setApproved($approved);

        if (!is_null($member)) {
            $review->setProfile($member);
        }

        $this->getEntityManager()->persist($review);
        $this->getEntityManager()->flush();

        return $review;
    }
}
