<?php
    
    namespace common\models;
    
    use Yii;
    use yii\behaviors\TimestampBehavior;

    /**
     * This is the model class for table "question".
     *
     * @property int     $id
     * @property string  $name
     * @property string  $email
     * @property string  $question
     * @property bool    $status
     * @property int     $service_id
     * @property int     $created_at
     * @property int     $updated_at
     * @property Service $service
     */
    class Question extends \yii\db\ActiveRecord
    {
        const SCENARIO_QUESTION = 'question';
        const SCENARIO_ANSWER = 'answer';
        /**
         * {@inheritdoc}
         */
        public static function tableName()
        {
            return 'question';
        }
        
        /**
         * @inheritdoc
         */
        public function scenarios()
        {
            $scenarios = parent::scenarios();
            $scenarios = array_merge(
                $scenarios,
                [
                    self::SCENARIO_QUESTION => [
                        'name',
                        'email',
                        'question',
                        'service_id'
                    ],
                    self::SCENARIO_ANSWER => [
                        'answer',
                        'status',
                        'doctor_id'
                    ],
                ]
            );
            return $scenarios;
        }
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                [
                    'class'              => TimestampBehavior::className(),
                ],
            ];
        }
        /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                [
                  ['name', 'email', 'question'], 'required', 'on' => self::SCENARIO_QUESTION
                ],
                [
                    [ 'status' ],
                    'boolean',
                ],
                [
                    [
                        'service_id',
                        'created_at',
                        'updated_at',
                    ],
                    'default',
                    'value' => null,
                ],
                [
                    [
                        'service_id',
                        'created_at',
                        'updated_at',
                        'doctor_id'
                    ],
                    'integer',
                ],
                [
                    [
                        'name',
                        'email',
                        
                    ],
                    'string',
                    'max' => 255,
                ],
                [
                  ['question', 'answer'], 'string'
                ],
                [
                    [ 'service_id' ],
                    'exist',
                    'skipOnError'     => true,
                    'targetClass'     => Service::className(),
                    'targetAttribute' => [ 'service_id' => 'id' ],
                ],
                [
                    'email', 'email'
                ]
            ];
        }
        
        /**
         * {@inheritdoc}
         */
        public function attributeLabels()
        {
            return [
                'id'         => Yii::t('app', 'ID'),
                'name'       => Yii::t('app', 'Name'),
                'email'      => Yii::t('app', 'Email'),
                'question'   => Yii::t('app', 'Question'),
                'status'     => Yii::t('app', 'Status'),
                'service_id' => Yii::t('app', 'Service ID'),
                'created_at' => Yii::t('app', 'Created At'),
                'updated_at' => Yii::t('app', 'Updated At'),
            ];
        }
        
        /**
         * @return \yii\db\ActiveQuery
         */
        public function getService()
        {
            return $this->hasOne(Service::className(), [ 'id' => 'service_id' ]);
        }
    
        public function getDoctor()
        {
            return $this->hasOne(Doctor::className(), [ 'id' => 'doctor_id' ]);
        }
    
        public function getEntityObject(){
            if ($this->entity !== null){
                return $this->hasOne($this->entity, ['id' => 'entity_id']);
            }
            return null;
        }
        
    }
