<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "branch".
 *
 * @property int $id
 * @property string $branch
 * @property int $branch_user_id
 * @property string $address
 * @property int $capacity
 *
 * @property Users $branchUser
 * @property TrainingRequest[] $trainingRequests
 */
class Branches extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'branches';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['branch', 'branch_english', 'branch_user_id', 'address', 'capacity'], 'required'],
            [['branch_user_id', 'capacity'], 'integer'],
            [['branch'], 'string', 'max' => 100],
            [['address'], 'string', 'max' => 500],
            [['branch'], 'match', 'pattern' => "/^([\x{0600}-\x{06ff}]|[\x{0750}-\x{077f}]|[\x{fb50}-\x{fbc1}]|[\x{fbd3}-\x{fd3f}]|[\x{fd50}-\x{fd8f}]|[\x{fd92}-\x{fdc7}]|[\x{fe70}-\x{fefc}]|[\x{fdf0}-\x{fdfd}]|[' '])*$/u", 'message' => Yii::t('app', '{attribute} has to be written in Arabic')],
            [['branch_english'], 'match', 'pattern' => "/^([A-Za-z]|[' '])*$/u", 'message' => Yii::t('app', '{attribute} has to be written in English')],
            [['branch_user_id'], 'exist', 'skipOnError' => true, 'targetClass' => Users::className(), 'targetAttribute' => ['branch_user_id' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'branch' => Yii::t('app', 'Branch'),
            'branch_english' => Yii::t('app', 'Branch English'),
            'branch_user_id' => Yii::t('app', 'Branch Admin'),
            'address' => Yii::t('app', 'Address'),
            'capacity' => Yii::t('app', 'Capacity'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getBranchUser()
    {
        return $this->hasOne(Users::className(), ['id' => 'branch_user_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTrainings()
    {
        return $this->hasMany(Trainings::className(), ['branch_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getInterviews()
    {
        return $this->hasMany(Interviews::className(), ['branch_id' => 'id']);
    }
}
