<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

/**
 * @property integer $user_id
 * @property integer $user_role_id
 * @property string $msisdn
 * @property string $email
 * @property string $name
 * @property int $institution_id
 * @property string $created
 * @property integer $status
 * @property string $modified
 * @property string $created_by
 * @property string $password
 * @property string $email_verified_at
 * @property string $approved_by
 * @property UserRole $userRole
 * @property Category[] $categories
 * @property Constituency[] $constituencies
 * @property ControlFigure[] $controlFigures
 * @property County[] $counties
 * @property CountyAssembly[] $countyAssemblies
 * @property FinancialYear[] $financialYears
 * @property Institution[] $institutions
 * @property Questionnaire[] $questionnaires
 * @property QuestionnaireAnswer[] $questionnaireAnswers
 * @property QuestionnaireAnswer[] $questionnaireAnswers
 * @property QuestionnaireAnswer[] $questionnaireAnswers
 * @property QuestionnaireAnswerDetail[] $questionnaireAnswerDetails
 * @property QuestionnairePart[] $questionnaireParts
 * @property QuestionnaireSection[] $questionnaireSections
 * @property QuestionnaireTable[] $questionnaireTables
 * @property SubCategory[] $subCategories
 * @property SubQuestionnaire[] $subQuestionnaires
 * @property UserSession[] $userSessions
 */
class User extends Authenticatable
{

    use Notifiable;

    /**
     * The table associated with the model.
     * 
     * @var string
     */
    protected $table = 'user';

    /**
     * The primary key for the model.
     * 
     * @var string
     */
    protected $primaryKey = 'user_id';

    /**
     * The "type" of the auto-incrementing ID.
     * 
     * @var string
     */
    protected $keyType = 'integer';

    public $timestamps = false;

    /**
     * @var array
     */
    protected $fillable = ['user_role_id', 'msisdn', 'email', 'name',
     'email_verified_at', 'institution_id', 'created', 'status', 'modified',
      'created_by', 'password', 'approved_by'];

    const QUESTIONNAIRE_ANSWER_MODEL = 'App\Models\QuestionnaireAnswer';

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function userRole()
    {
        return $this->belongsTo('App\Models\UserRole', 'user_role_id', 'user_role_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function institution()
    {
        return $this->belongsTo('App\Models\Institution', 'institution_id', 'institution_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function categories()
    {
        return $this->hasMany('App\Models\Category', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function constituencies()
    {
        return $this->hasMany('App\Models\Constituency', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function controlFigures()
    {
        return $this->hasMany('App\Models\ControlFigure', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function counties()
    {
        return $this->hasMany('App\Models\County', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function countyAssemblies()
    {
        return $this->hasMany('App\Models\CountyAssembly', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function financialYears()
    {
        return $this->hasMany('App\Models\FinancialYear', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function institutions()
    {
        return $this->hasMany('App\Models\Institution', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function questionnaires()
    {
        return $this->hasMany('App\Models\Questionnaire', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function questionnaireAnswersAnsweredBy()
    {
        return $this->hasMany(User::QUESTIONNAIRE_ANSWER_MODEL, 'answered_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function questionnaireAnswersApprovedBy()
    {
        return $this->hasMany(User::QUESTIONNAIRE_ANSWER_MODEL, 'approved_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function questionnaireAnswersCreatedBy()
    {
        return $this->hasMany(User::QUESTIONNAIRE_ANSWER_MODEL, 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function questionnaireAnswerDetails()
    {
        return $this->hasMany('App\Models\QuestionnaireAnswerDetail', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function questionnaireParts()
    {
        return $this->hasMany('App\Models\QuestionnairePart', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function questionnaireSections()
    {
        return $this->hasMany('App\Models\QuestionnaireSection', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function questionnaireTables()
    {
        return $this->hasMany('App\Models\QuestionnaireTable', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function subCategories()
    {
        return $this->hasMany('App\Models\SubCategory', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function subQuestionnaires()
    {
        return $this->hasMany('App\Models\SubQuestionnaire', 'created_by', 'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function userSessions()
    {
        return $this->hasMany('App\Models\UserSession', null, 'user_id');
    }
}