<?php

namespace App\Models;

use CodeIgniter\Model;

class BlogModel extends Model
{
    protected $db;
    protected $blog  = 'blog';
    protected $category  = 'blog_category';
    protected $blog_review  = 'blog_review';

    function get_all_blog()
    {
        return $this->db->table($this->blog)
        ->select('*')
        ->join('blog_category', 'blog_category.category_id = blog.category')
        ->get()
        ->getResult();
    }

    function get_all_blog_for_all_blog_page()
    {
        return $this->db->table($this->blog)
        ->select('title, title_slug, image, short_description, date, view')
        ->get()
        ->getResult();
    }

    function get_top_10_blog()
    {
        return $this->db->table($this->blog)
        ->select('*')
        ->join('blog_category', 'blog_category.category_id = blog.category')
        ->limit(10)
        ->orderBy('id','DESC')
        ->get()
        ->getResult();
    }

    function get_top_4_blog()
    {
        return $this->db->table($this->blog)
        ->select('title, title_slug, image, date, short_description, like, view')
        ->limit(3)
        ->orderBy('id','DESC')
        ->get()
        ->getResult();
    }

    function get_blog_by_id($id)
    {
        return $this->db->table($this->blog)
        ->where('id', $id)
        ->get()
        ->getRow();
    }

    function get_blog_by_slug($slug)
    {
       $info = $this->db->table($this->blog)
        ->where(['title_slug'=> $slug])
        ->get();
        $final = array();
        if ($info->getNumRows() > 0) 
        {
            foreach ($info->getResult() as $row) 
            {
                $info =  $this->db->table($this->category)
                 ->where(['category_id !=' => $row->category])
                ->get();
                $row->all_category = $info->getResult();

                $info =  $this->db->table($this->blog)
                ->select('title, title_slug, image, date')
                ->where('id !=', $row->id)
                ->orderBy('id', "DESC")
                ->limit(5)
                ->get();
                $row->recent_blog = $info->getResult();

                array_push($final, $row);
            }
        }
        return $final[0];
    }

    function add_new_blog($data)
    {
        return $this->db->table($this->blog)->insert($data);
    }

    function update_blog_details($data, $id)
    {
       return $this->db->table($this->blog)
       ->where(['id'=> $id])
       ->update($data);
    }

    function delete_blog($id)
    {
        return $this->db->table($this->blog)
        ->where(['id'=> $id])
        ->delete();
    }

    function get_all_blog_by_category_slug($slug)
    {
        return $this->db->table($this->category)
        ->select('*')
        ->join('blog', 'blog.category = blog_category.category_id')
        ->where('category_slug', $slug)
        ->get()
        ->getResult();
    }

    function get_all_category()
    {
        return $this->db->table($this->category)->get()->getResult();
    }

    function get_category_by_id($id)
    {
        return $this->db->table($this->category)
        ->where(['category_id'=> $id])
        ->get()
        ->getRow();
    }

    function get_category_by_slug($slug)
    {
        return $this->db->table($this->category)
        ->where(['category_slug'=> $slug])
        ->get()
        ->getRow();
    }

    function add_new_category($data)
    {
        return $this->db->table($this->category)->insert($data);
    }

    function update_category($data, $id)
    {
       return $this->db->table($this->category)
       ->where(['category_id'=> $id])
       ->update($data);
    }

    function delete_category($id)
    {
        return $this->db->table($this->category)
        ->where(['category_id'=> $id])
        ->delete();
    }

    function update_blog_view($data, $id)
    {
        return $this->db->table($this->blog)
        ->where('id', $id)
        ->update($data);
    }

    function submit_blog_review($data)
    {
        return $this->db->table($this->blog_review)->insert($data);
    }

     function get_all_blog_review()
    {
        return $this->db->table($this->blog_review)->get()->getResult();
    }

    function delete_blog_review($id)
    {
        return $this->db->table($this->blog_review)
        ->where(['id'=> $id])
        ->delete();
    }

    function get_all_blog_three_for_frontend()
    {
        return $this->db->table($this->blog)
        ->select('title, title_slug, short_description, date, view, image')
        ->limit(3)
        ->get()
        ->getResult();
    }

    function get_top_5_blog()
    {
        return $this->db->table($this->blog)
        ->select('title, title_slug, date, image')
        ->limit(5)
        ->orderBy('id', "desc")
        ->get()
        ->getResult();
    }

    function get_all_blog_for_frontend()
    {
        return $this->db->table($this->blog)
        ->select('title, title_slug, short_description, date, view, image')
        ->get()
        ->getResult();
    }

    function get_all_blog_by_category_slug_for_frontend($slug)
    {
        $cat_id = $this->get_category_by_slug($slug);
        return $this->db->table($this->blog)
        ->select('title, title_slug, short_description, date, view, image')
        ->where('category', $cat_id->category_id)
        ->get()
        ->getResult();
    }

    function get_single_blog_for_frontend()
    {
        return $this->db->table($this->blog)
        ->select('title, title_slug, short_description, image')
        ->orderBy('id', 'RANDOM')
        ->limit(1)
        ->get()
        ->getRow();
    }

    function get_blog_by_id_for_like_system($id) 
    {
        return $this->db->table($this->blog)
        ->select('like, id, dislike, love')
        ->where('id', $id)
        ->get()
        ->getRow();
    }

    //save reaction
    public function save_reaction($post_id, $reaction)
    {
        if (is_reaction_voted($post_id, $reaction)) 
        {
            helper_setsession('reaction_' . $reaction . '_' . $post_id, '0');
            $this->set_cookie_reaction('reaction_' . $reaction . '_' . $post_id, '0');
            $this->decrease_reaction_vote($post_id, $reaction);
            $this->decrease_post_vote_session($post_id);
        } 
        else 
        {
            helper_setsession('reaction_' . $reaction . '_' . $post_id, '1');
            $this->set_cookie_reaction('reaction_' . $reaction . '_' . $post_id, '1');
            $this->increase_reaction_vote($post_id, $reaction);
            $this->increase_post_vote_session($post_id);
        }
    }

      //increase reaction vote
      public function increase_reaction_vote($post_id, $reaction)
      {
        $row = $this->get_blog_by_id_for_like_system($post_id);
        $re = $reaction;
        $data = array(
              ''.$reaction.'' => $row->$re + 1,
        );

        $this->db->table($this->blog)
        ->where('id', $post_id)
        ->update($data);
      }

      //decrease reaction vote
      public function decrease_reaction_vote($post_id, $reaction)
      {
        $row = $this->get_blog_by_id_for_like_system($post_id);
        $re = $reaction;
        $data = array(
              ''.$reaction.'' => $row->$re - 1,
        );

        $this->db->table($this->blog)
        ->where('id', $post_id)
        ->update($data);
      }

      //increase post vote session
      public function increase_post_vote_session($post_id)
      {
          //vote count
          $vote = helper_getcookie('reaction_vote_count_' . $post_id, 'int');
          helper_setsession('reaction_vote_count_' . $post_id, $vote + 1);
          $this->set_cookie_reaction('reaction_vote_count_' . $post_id, $vote + 1);
      }

      //decrease post vote session
      public function decrease_post_vote_session($post_id)
      {
            $vote = helper_getcookie('reaction_vote_count_' . $post_id, 'int');
            helper_setsession('reaction_vote_count_' . $post_id, $vote - 1);
            $this->set_cookie_reaction('reaction_vote_count_' . $post_id, $vote - 1);
      }


      public function set_cookie_reaction($name, $value)
      {
          setcookie($name, $value, time() + (86400 * 365), "/");
      }

    
}
