<?php

namespace common\models;

use common\components\RoleDefinition;
use common\helpers\AppArrayHelper;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "trainer".
 *
 * @property integer $id
 * @property string $name
 * @property string $phone
 * @property string $email
 * @property string $password
 * @property integer $active
 * @property string $created_at
 * @property string $updated_at
 */
class Trainer extends \yii\db\ActiveRecord
{
    const ACTIVE_ON = 1;
    const ACTIVE_OFF = 0;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'trainer';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['active'], 'integer'],
            [['name', 'email'], 'string', 'max' => 255],
            [['phone'], 'string', 'max' => 20],
            [['name','phone','email'] , 'required'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('trainer', 'ID'),
            'name' => Yii::t('trainer', 'Name'),
            'phone' => Yii::t('trainer', 'Phone'),
            'email' => Yii::t('trainer', 'Email'),
            'password' => Yii::t('trainer', 'Password'),
            'active' => Yii::t('trainer', 'Active'),
            'created_at' => Yii::t('trainer', 'Created At'),
            'updated_at' => Yii::t('trainer', 'Updated At'),
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(  [
            [
                'class' => TimestampBehavior::className(),
                'value' => function(){ return date('Y-m-d H:i:s' ); }
            ]
        ],
            parent::behaviors());
    }

    public static function prettyPrintActive($active){
        if ( $active == Trainer::ACTIVE_ON ){
            return \Yii::t("trainer",'active_on');
        }
        return \Yii::t("trainer",'active_off');
    }

    public function getUserTrainerAssignments()
    {
        return $this->hasMany(UserTrainerAssignment::class, ['id_trainer' => 'id']);
    }


    public static function trainerOptions($all = false, $emptyString = false, $trainers = null){

        if ( !isset($trainers)){
            $trainers = Trainer::find()->all();
        }
       $items =  ArrayHelper::map($trainers,'id','name');
        $extra = [];
        if ( $all ) {
            $extra = ['' => \Yii::t('trainer','All')];
        }
        if ( $emptyString ) {
            $extra = ['' => '' ];
        }
        return ArrayHelper::merge($extra,$items);
    }

    public static function getTrainersAllowed($idUser){
        $query = Trainer::find();
        if (RoleDefinition::isAdmin() == false) {
            $query = $query->innerJoinWith('userTrainerAssignments')
                ->andWhere(
                    [
                        'user_trainer_assignment.id_user' => $idUser
                    ]
                );
        }
        return $query->all();
    }

    public static function isTrainerAllowed($trainers,$idUser,$idTrainer){
        $trainerAllowed = false;
        foreach ($trainers as $trainer){
            if ( $trainer->id == $idTrainer){
                $trainerAllowed = true;
            }
        }
        return $trainerAllowed;
    }

}
