<?php


namespace Database;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Capsule\Manager as DB;

class Follow extends Model
{
    protected $table = 'follow';

    public static function getFollows($chatid, $limit = true, $page = 1, $per = 20)
    {
        if ($limit) {
            $pageNumber = ($page - 1) * $per;
            return DB::table('follow')->where('chatid', (string)$chatid)->limit($per)->offset($pageNumber)->orderBy('id', 'DESC')->get();

        } else {
            return DB::table('follow')->where('chatid', (string)$chatid)->get();
        }
    }

    public static function hasFollow($chatid)
    {
        return DB::table('follow')->where('chatid',(string) $chatid)->exists();
    }

    public static function countFollow($chatid)
    {
        return DB::table('follow')->select('id')->where('chatid', (string)$chatid)->count();
    }

    public static function checkFollows($chatid, $userid)
    {
        return DB::table('follow')->where('chatid', (string)$chatid)->where('userid', (string)$userid)->exists();
    }

    public static function addFllow($chatid, $userid)
    {
        return DB::table('follow')->insertGetId(['chatid' => (string)$chatid, 'userid' =>(string) $userid]);
    }

    public static function removeFollow($chatid, $userid)
    {
        return DB::table('follow')->where('chatid',(string) $chatid)->where('userid',(string) $userid)->delete();
    }

    public static function removeFollowById($id)
    {
        return DB::table('follow')->where('id', $id)->delete();
    }

    public static function getFollow($chatid, $userid)
    {
        return DB::table('bot_ads')->where('chatid', (string)$chatid)->where('userid', (string)$userid)->first();
    }
}
