<?php


namespace Database;

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

class Question extends Model
{
    protected $table = 'question';
    public $timestamps = false;

    public static function getQuestion($id)
    {
        return DB::connection('sqlite')->table('question')->select('id', 'question')->where('type', $id)->get()->toArray();
    }


    public static function getQuestionRandomNotsex($id)
    {
        return DB::connection('sqlite')->table('question')->select('id', 'question')->where('type', $id)->inRandomOrder()->limit(100)->get()->toArray();
    }

    public static function getBygender($sex = 'all')
    {
        return DB::connection('sqlite')->table('question')->select('id', 'question')->where('sex', $sex)->get()->toArray();

    }

    public static function getBygenderAndType($type, $sex = 'all')
    {
        return DB::connection('sqlite')->table('question')->select('id', 'question')->where('type', $type)->where('sex', $sex)->get()->toArray();
    }

    public static function getQuestionRandom($id, $sex = 'all')
    {
        return DB::connection('sqlite')->table('question')->select('id', 'question')->where('type', $id)->where(function ($q) use ($sex) {
            $q->where('sex', $sex)->orWhere('sex', 'all');
        })->inRandomOrder()->limit(100)->get()->toArray();
    }

    public static function addQuestion($id, $text, $sex)
    {
        return DB::connection('sqlite')->table('question')->insertGetId(['type' => $id, 'question' => $text, 'sex' => $sex]);
    }

    public static function hasQuestion($id, $text, $sex)
    {
        return DB::connection('sqlite')->table('question')->where('type', $id)->where('question', $text)->where('sex', $sex)->exists();
    }

    public static function countQuestion($id)
    {
        return DB::connection('sqlite')->table('question')->select('id')->where('type', $id)->count();
    }

    public static function removeQuestion($id, $text)
    {
        return DB::connection('sqlite')->table('question')->where('type', $id)->where('question', $text)->delete();
    }

    public static function removeById($id)
    {
        return DB::connection('sqlite')->table('question')->where('id', $id)->delete();
    }
}
