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


class Pray_model extends MY_Model
{

    private $table = 'cm_pray';

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

    public function getPrayQuery()
    {
        return $this->db->from($this->table)->order_by('dateUpdated', 'desc');
    }

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

    public function getCountPaging()
    {
        if (!isAdminSite()) {
            $this->db->where('dateDeleted is null');
        }
        return $this->db->from($this->table)
            ->count_all_results();
    }

    public function getAll($limit, $offiset)
    {
        return $this->getPostQuery()->limit($limit)->offset($offiset);
    }

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

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

    public function update($prayId, $data)
    {
        $this->db->where('prayID', $prayId)->update($this->table, $data);
        return $this->db->affected_rows();
    }

    public function delete($prayId)
    {
        return $this->db->where('prayID', $prayId)->delete($this->table);
    }

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

    public function getBookmarksAndRankings(&$prayers)
    {
        if (count($prayers)) {
            $userID = getSessionUser()->is ? getSessionUser()->userID : 0;
            array_map(function ($rating) use (&$prayers, $userID) {
                foreach ($prayers as $k => $v) {
                    if ($v->prayID == $rating->prayID) {
                        if ($rating->title == 1) {
                            $prayers[$k]->like++;
                        } else if($rating->title == 3){
                            if($rating->userID == $userID) {
                                $prayers[$k]->bookmark = 1;    
                            }
                            $prayers[$k]->bookmarks++;
                        }
                    }
                }
            }, $this->getRankingsByPrayId(array_column($prayers, 'prayID')));
        }
    }

    private function getRankingsByPrayId($prayId)
    {
        $prayId = is_array($prayId) ? $prayId : [$prayId];
        return $this->db->where_in('prayID', $prayId)
            ->get('cm_pray_ranking')->result();
    }

    public function getPrayRankingCountByTitle($prayId, $title) {
        return $this->db->where('prayID', $prayId)->where('title', $title)->count_all_results('cm_pray_ranking');
    }
}
