<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}


class Comment_model extends MY_Model
{

    private $table = 'cm_comment';

    public function __construct()
    {
        parent::__construct();
    }

    public function getPaging($limit, $offset = 0)
    {
        if (!isAdminSite()) {
            $this->db->where('dateDeleted', '0000-00-00 00:00:00')->where('parentID', 0);
        }
        return $this->db->from($this->table . ' c')
            ->select("c.*, if(u.nickname = '' or u.nickname is null, u.userName, u.nickName) as nickname")
            ->join('cm_user u', 'c.userID=u.userID', 'left')
            ->order_by('datePosted', 'desc')
            ->limit($limit)->offset($offset);
    }

    public function getCountPaging()
    {
        if (!isAdminSite()) {
            $this->db->where('dateDeleted', '0000-00-00 00:00:00');
        }
        return $this->db->from($this->table)
            ->count_all_results();
    }

    public function getOne($commentId)
    {
        return $this->db->where('commentID', $commentId)->get($this->table)->row();
    }

    public function getAll($commentId)
    {
        $commentId = is_array($commentId) ? $commentId : [$commentId];
        return $this->db->where_in('commentId', $commentId)->get($this->table)->result();
    }

    public function getByParentId(int $parentID)
    {
        return $this->db->where('parentID', $parentID)->get($this->table)->result();
    }

    public function getByPostId($postId)
    {
        $postId = is_array($postId) ? $postId : [$postId];
        return $this->db->where_in('postID', $postId)
            ->where('dateDeleted', '0000-00-00 00:00:00')
            ->order_by('datePosted', 'desc')->from($this->table);
    }

    public function getRepliesByCommentId($commentId)
    {
        $commentId = is_array($commentId) ? $commentId : [$commentId];
        return $this->db->where_in('parentID', $commentId)
            ->where('dateDeleted', '0000-00-00 00:00:00')
            ->order_by('datePosted', 'desc')->from($this->table);
    }

    public function getCountByPostId($postId)
    {
        $postId = is_array($postId) ? $postId : [$postId];
        return $this->db->where_in('postID', $postId)
            ->where('dateDeleted !=', '0000-00-00 00:00:00')
            ->group_by('postID')->select('count(postID) as cnt, postID')
            ->from($this->table);
    }

    public function save($data)
    {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function update($commentId)
    {
        $data = [
            'comment' => $this->input->post('comment'),
            'dateUpdated' => date('Y-m-d H:i:s')
        ];
        $this->db->where('commentID', $commentId)
            ->where('userID', $this->session->userdata('userID'))
            ->update($this->table, $data);
        return $this->db->affected_rows();
    }

    public function delete($commentId)
    {
        if (isAdminSite()) { 
            return $this->db->where('commentID', $commentId)->delete($this->table);
        } else {
            $data = [
                'dateDeleted' => date('Y-m-d H:i:s')
            ];
            $this->db->where('commentID', $commentId)->update($this->table, $data);
            return $this->db->affected_rows();
        }
    }

    public function rating($data)
    {
        $this->db->insert('cm_comment_ranking', $data);
        return $this->db->insert_id();
    }

    public function getCountRatingByCommentIdAndTitle($commentId, $title)
    {
        return $this->db->where('title', $title)
            ->where('commentID', $commentId)
            ->count_all_results('cm_comment_ranking');
    }

    public function getCommentRatingsByCommentId($commentId)
    {
        $commentId = is_array($commentId) ? $commentId : [$commentId];
        return $this->db
            ->where_in('commentID', $commentId)
            ->get('cm_comment_ranking')->result();
    }

    public function getRepliesAndRankings(&$comments)
    {
        if (count($comments)) {
            array_map(function ($rating) use (&$comments) {
                foreach ($comments as $k => $v) {
                    if ($v->commentID == $rating->commentID) {
                        if ($rating->title == 1) {
                            $comments[$k]->like++;
                        } else {
                            $comments[$k]->bad++;
                        }
                    }
                }
            }, $this->getCommentRatingsByCommentId(array_column($comments, 'commentID')));

            array_map(function ($reply) use (&$comments) {
                foreach ($comments as $k => $v) {
                    if ($v->commentID == $reply->parentID) {
                        $comments[$k]->replies = $reply->replies;
                    }
                }
            }, $this->getRepliesByCommentId(array_column($comments, 'commentID'))
                ->select('count(commentID) replies, parentID')->group_by('parentID')->get()->result());
        }
    }
}
