<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * MobileLines Model
 *
 * @property \App\Model\Table\UsersTable|\Cake\ORM\Association\BelongsTo $Users
 * @property \App\Model\Table\BillsTable|\Cake\ORM\Association\HasMany $Bills
 * @property \App\Model\Table\ContractsTable|\Cake\ORM\Association\HasMany $Contracts
 *
 * @method \App\Model\Entity\MobileLine get($primaryKey, $options = [])
 * @method \App\Model\Entity\MobileLine newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\MobileLine[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\MobileLine|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\MobileLine patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\MobileLine[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\MobileLine findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class MobileLinesTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('mobile_lines');
        $this->setDisplayField('id');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'LEFT'
        ]);
        $this->hasMany('Bills', [
            'foreignKey' => 'mobile_line_id'
        ]);
        $this->hasMany('Contracts', [
            'foreignKey' => 'mobile_line_id'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->uuid('id')
            ->allowEmpty('id', 'create');

        $validator
            ->scalar('msisid')
            ->maxLength('msisid', 20)
            ->allowEmpty('msisid');

        $validator
            ->scalar('imsi')
            ->maxLength('imsi', 20)
            ->allowEmpty('imsi');

        $validator
            ->integer('type')
            ->allowEmpty('type');

        $validator
            ->scalar('iccid')
            ->maxLength('iccid', 20)
            ->allowEmpty('iccid');

        $validator
            ->dateTime('available_after')
            ->allowEmpty('available_after');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['user_id'], 'Users'));

        return $rules;
    }
}
