<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "courier".
 *
 * @property int $id
 * @property int $user_id
 * @property string $passport
 * @property string $driver_license
 * @property string $auto
 * @property string $auto_number
 * @property int $status
 *
 * @property Order[] $orders
 * @property User $user
 */
class Courier extends \yii\db\ActiveRecord
{

    const STATUS_FREE = 0;
    const STATUS_BUSY = 1;

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

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['status'], 'default', 'value' => 0],
            [['user_id', 'passport', 'driver_license', 'auto', 'auto_number'], 'required'],
            [['user_id', 'status'], 'integer'],
            [['passport', 'driver_license', 'auto'], 'string', 'max' => 256],
            [['auto_number'], 'string', 'max' => 6],
            [['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']],
            [['telegram_id'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'user_id' => 'User ID',
            'passport' => 'Passport',
            'driver_license' => 'Driver License',
            'auto' => 'Auto',
            'auto_number' => 'Auto Number',
            'status' => 'Status',
        ];
    }

    /**
     * Gets query for [[Orders]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getOrders()
    {
        return $this->hasMany(Order::class, ['courier_id' => 'id']);
    }

    /**
     * Gets query for [[User]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(User::class, ['id' => 'user_id']);
    }
    public function getDeliveries()
    {
        return $this->hasMany(Delivery::class, ['courier_id' => 'id']);
    }
// В модели Courier добавляем метод
    public function generateTelegramToken()
    {
    $this->telegram_token = Yii::$app->security->generateRandomString(32);
    $this->save(false);
    return $this->telegram_token;
}

}
