<?php

namespace Database;

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


class Users extends Model
{
    protected $table = 'users';
    protected $primaryKey = 'chatid';
    protected $casts = ['active' => 'boolean'];

    public static function checkAndInsert($chatid)
    {
        if (!self::check($chatid)) {
            return DB::table('users')->insertGetId(['chatid' => $chatid]);
        }
        return false;
    }

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

    public static function getAllActiveUser($limit = true, $page = 1, $per = 20)
    {
        if ($limit) {
            $pageNumber = ($page - 1) * $per;

            return DB::table('users')->where('active', true)->orderBy('id', 'DESC')->paginate($per,['*'],'page',$page);

        }

        return DB::table('users')->where('active', true)->get();

    }
    public static function getAllStatusActiveUser2($limit = true, $page = 1, $per = 20)
    {
        if ($limit) {
            $pageNumber = ($page - 1) * $per;

            return DB::table('users')->where('status', true)->orderBy('id')->paginate($per, ['*'],'page',$page);

        }

        return DB::table('users')->where('status', true)->get();

    }

    public static function getAllStatusActiveUser($limit = true, $page = 1, $per = 20)
    {
        if ($limit) {
            $pageNumber = ($page - 1) * $per;
            return DB::table('users')->where('status', true)->limit($per)->offset($pageNumber)->orderBy('id')->get();
        }

        return DB::table('users')->where('status', true)->get();

    }

    public static function getAll($limit = true, $page = 1, $per = 20)
    {
        if (!$limit) {
            return DB::table('users')->get();
        }
        $pageNumber = ($page - 1) * $per;
        return DB::table('users')->limit($per)->offset($pageNumber)->orderBy('id')->get();

    }
    public static function getAllWeek($limit = true, $page = 1, $per = 20)
    {
        if (!$limit) {
            return DB::table('users')->where('giveWeek', '<', Carbon::now('Asia/Tehran')->subWeek()->toDateTimeString())->where('status',1)->where('update_at', '<', Carbon::now('Asia/Tehran')->subWeek()->toDateTimeString())->get();
        }
        $pageNumber = ($page - 1) * $per;
        return DB::table('users')->where('giveWeek', '<', Carbon::now('Asia/Tehran')->subWeek()->toDateTimeString())->where('status',1)->where('update_at', '<', Carbon::now('Asia/Tehran')->subWeek()->toDateTimeString())->limit($per)->offset($pageNumber)->orderBy('id')->get();

    }

    public static function getAdmins()
    {
        return DB::table('users')->select('id', 'chatid')->where('rank', 'admin')->get();
    }

    public static function getCountActive()
    {
        return DB::table('users')->select('id')->where('status', true)->count();
    }

    public static function getCountNotActive()
    {
        return DB::table('users')->select('users.id')->where('status', false)->count();
    }

    public static function getCountAll()
    {
        return DB::table('users')->select('users.id')->count();
    }

    public static function getUser($chatid)
    {
        return DB::table('users')->where('chatid', $chatid)->first();
    }

    public static function getSelectGender($chatid)
    {
        return DB::table('users')->select('users.selectGender')->where('chatid', $chatid)->first();
    }

    public static function getCountYesterdaysingUp()
    {
        return DB::table('users')->select('users.id')->where('create_at', '>', Carbon::yesterday('Asia/Tehran')->toDateTimeString())->count();
    }

    public static function getCountTodayingUp()
    {
        return DB::table('users')->select('users.id')->where('create_at', '>', Carbon::today('Asia/Tehran')->toDateTimeString())->count();
    }

    public static function getCountWeekSingUp()
    {
        return DB::table('users')->select('users.id')->where('create_at', '>', Carbon::now('Asia/Tehran')->subWeek()->toDateTimeString())->count();
    }

    public static function getCountMonthSingUp()
    {
        return DB::table('users')->select('users.id')->where('create_at', '>', Carbon::now('Asia/Tehran')->subMonth()->toDateTimeString())->count();
    }

    public static function updateCommand($cahtid, $values)
    {
        DB::table('users')->where('chatid', $cahtid)->update(['command' => $values]);
        return true;
    }

    public static function updateData($cahtid, $values)
    {
        DB::table('users')->where('chatid', $cahtid)->update(['data' => $values]);
        return true;
    }

    public static function updateRank($cahtid, $values)
    {
        DB::table('users')->where('chatid', $cahtid)->update(['rank' => $values]);
        return true;
    }

    public static function updateActive($cahtid, $values)
    {
        DB::table('users')->where('chatid', $cahtid)->update(['active' => $values]);
        return true;
    }

    public static function updateBot($cahtid, $values)
    {
        DB::table('users')->where('chatid', $cahtid)->update(['bot' => $values]);
        return true;
    }

    public static function updateStatus($cahtid, $values)
    {
        DB::table('users')->where('chatid', $cahtid)->update(['status' => $values]);
        return true;
    }

    public static function updateAds($cahtid, $values)
    {
        DB::table('users')->where('chatid', $cahtid)->update(['ads' => $values]);
        return true;
    }
    public static function updateGiveWeek($cahtid)
    {
        DB::table('users')->where('chatid', $cahtid)->update(['giveWeek' => Carbon::now('Asia/Tehran')]);
        return true;
    }

    public static function updateSelectGender($cahtid, $values)
    {
        DB::table('users')->where('chatid', $cahtid)->update(['selectGender' => $values]);
        return true;
    }

}
