<?php
/**
 * @author  Magediary
 * @package Magediary_ProductQuestion
 */

namespace Magediary\ProductQuestion\Model\ResourceModel;

use Magediary\ProductQuestion\Model\ActivityLog as ActivityLogModel;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;

class ActivityLog extends AbstractDb
{
    /**
     * Define resource model
     *
     * @return void
     */
    protected function _construct()
    {
        $this->_init('magediary_answer_activitylog', 'log_id');
    }

    /**
     * Revert likes
     *
     * @param int|string $id
     * @param int $customerId
     * @return $this
     * @throws LocalizedException
     */
    public function revertLikes($id, $customerId)
    {
        $table = $this->getMainTable();

        $condition = [];
        $condition[] = $this->getConnection()->quoteInto('customer_id = ?', $customerId);
        $condition[] = $this->getConnection()->quoteInto('answer_id = ?', (int)$id);
        $condition[] = $this->getConnection()->quoteInto('activity_type != ?', ActivityLogModel::ACTIVITY_ABUSE);

        $deleteCondition = implode(' AND ', $condition);

        $this->getConnection()->delete(
            $table,
            $deleteCondition
        );

        return $this;
    }

    /**
     * Add like
     *
     * @param int $id
     * @param int $customerId
     * @param int $productId
     * @return $this
     * @throws LocalizedException
     */
    public function addLikeLog($id, $customerId = 0, $productId = 0)
    {
        $connection = $this->getConnection();
        $table = $this->getMainTable();

        $data = [
            'answer_id' => $id,
            'customer_id' => $customerId,
            'product_id' => $productId,
            'activity_type' => ActivityLogModel::ACTIVITY_LIKE
        ];

        $connection->insert($table, $data);

        return $this;
    }

    /**
     * Get likes count
     *
     * @param int $id
     * @return string
     * @throws LocalizedException
     */
    public function getLikeCount($id)
    {
        $connection = $this->getConnection();
        $table = $this->getMainTable();

        $select = $connection->select()
            ->from($table, 'COUNT(*)')
            ->where('answer_id = ?', $id)
            ->where('activity_type = ?', ActivityLogModel::ACTIVITY_LIKE);

        return $connection->fetchOne($select);
    }

    /**
     * Add dislike log
     *
     * @param int $id
     * @param int $customerId
     * @param int $productId
     * @return $this
     * @throws LocalizedException
     */
    public function addDislikeLog($id, $customerId = 0, $productId = 0)
    {
        $connection = $this->getConnection();
        $table = $this->getMainTable();

        $data = [
            'answer_id' => $id,
            'customer_id' => $customerId,
            'product_id' => $productId,
            'activity_type' => ActivityLogModel::ACTIVITY_DISLIKE
        ];

        $connection->insert($table, $data);

        return $this;
    }

    /**
     * Get dislike count
     *
     * @param int $id
     * @return string
     * @throws LocalizedException
     */
    public function getDislikeCount($id)
    {
        $connection = $this->getConnection();
        $table = $this->getMainTable();

        $select = $connection->select()
            ->from($table, 'COUNT(*)')
            ->where('answer_id = ?', $id)
            ->where('activity_type = ?', ActivityLogModel::ACTIVITY_DISLIKE);

        return $connection->fetchOne($select);
    }

    /**
     * Report abuse
     *
     * @param int $id
     * @param int $customerId
     * @param int $productId
     * @return $this
     * @throws LocalizedException
     */
    public function reportAbuse($id, $customerId = 0, $productId = 0)
    {
        $connection = $this->getConnection();
        $table = $this->getMainTable();

        $data = [
            'answer_id' => $id,
            'customer_id' => $customerId,
            'product_id' => $productId,
            'activity_type' => ActivityLogModel::ACTIVITY_ABUSE
        ];

        $connection->insert($table, $data);

        return $this;
    }

    /**
     * Get abuse count of customer
     *
     * @param int $id
     * @param int $customerId
     * @return string
     * @throws LocalizedException
     */
    public function getAbuseCount($id, $customerId = 0)
    {
        $connection = $this->getConnection();
        $table = $this->getMainTable();

        $select = $connection->select()
            ->from($table, 'COUNT(*)')
            ->where('answer_id = ?', $id)
            ->where('customer_id = ?', $customerId)
            ->where('activity_type = ?', ActivityLogModel::ACTIVITY_ABUSE);

        return $connection->fetchOne($select);
    }
}
