<?php
namespace App\Model\Table;

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

/**
 * Freelancers Model
 *
 * @property \Cake\ORM\Association\BelongsTo $Skyps
 * @property \Cake\ORM\Association\HasMany $Bids
 * @property \Cake\ORM\Association\HasMany $EssayTest
 * @property \Cake\ORM\Association\HasMany $Orders
 * @property \Cake\ORM\Association\HasMany $Results
 * @property \Cake\ORM\Association\HasMany $Transactions
 *
 * @method \App\Model\Entity\Freelancer get($primaryKey, $options = [])
 * @method \App\Model\Entity\Freelancer newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Freelancer[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Freelancer|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Freelancer patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Freelancer[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Freelancer findOrCreate($search, callable $callback = null, $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class FreelancersTable extends Table
{

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

        $this->table('freelancers');
        $this->displayField('name');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');

        $this->addBehavior('Upload');
        $this->addBehavior('Upload');
        $this->hasMany('Bids', [
            'foreignKey' => 'freelancer_id'
        ]);
        $this->belongsTo('Projects', [
            'foreignKey' => 'project_id',
            'through' => 'Bids'
        ]);
        $this->hasMany('EssayTest', [
            'foreignKey' => 'freelancer_id'
        ]);
        $this->hasMany('CategoryToFreelancer', [
            'foreignKey' => 'freelancer_id'
        ]);
         $this->hasMany('CategoryToFreelancer', [
            // 'bindingKey' => 'freelancer_id',
            // 'foreignKey' => 'id',
            'joinType' => 'right',
            
        ]);
        // $this->hasMany('Orders', [
        //     'foreignKey' => 'freelancer_id'
        // ]);
        $this->hasMany('Results', [
            'foreignKey' => 'freelancer_id'
        ]);
        $this->hasMany('Transactions', [
            'foreignKey' => 'freelancer_id'
        ]);
        $this->hasMany('Projects', [
            'foreignKey' => 'freelancer_id'
        ]);
        $this->belongsTo('Category', [
            'foreignKey' => 'category_id'
        ]);
      
    }

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

        $validator
            ->requirePresence('name', 'create')
            ->notEmpty('name');

        $validator
            ->email('email')
            ->requirePresence('email', 'create')
            ->notEmpty('email');

        $validator
            ->requirePresence('mobile', 'create')
            ->notEmpty('mobile');

        // $validator
        //     ->requirePresence('password', 'create')
        //     ->notEmpty('password');

        // $validator
        //     ->requirePresence('country', 'create')
        //     ->notEmpty('country');

        // $validator
        //     ->requirePresence('state', 'create')
        //     ->notEmpty('state');

        // $validator
        //     ->requirePresence('city', 'create')
        //     ->notEmpty('city');

        // $validator
        //     ->integer('pin_code')
        //     ->requirePresence('pin_code', 'create')
        //     ->notEmpty('pin_code');

        // $validator
        //     ->requirePresence('address', 'create')
        //     ->notEmpty('address');

        // $validator
        //     ->integer('time_zone')
        //     ->requirePresence('time_zone', 'create')
        //     ->notEmpty('time_zone');

        // $validator
        //     ->requirePresence('address_proof', 'create')
        //     ->notEmpty('address_proof');

        // $validator
        //     ->requirePresence('language', 'create')
        //     ->notEmpty('language');

        // $validator
        //     ->integer('group_work_status')
        //     ->requirePresence('group_work_status', 'create')
        //     ->notEmpty('group_work_status');

        // $validator
        //     ->integer('experience')
        //     ->requirePresence('experience', 'create')
        //     ->notEmpty('experience');

        // $validator            
        //     ->requirePresence('degree', 'create')
        //     ->notEmpty('degree');

        // $validator            
        //     ->requirePresence('proficient', 'create')
        //     ->notEmpty('proficient');

        // $validator
        //     ->integer('paypal')
        //     ->requirePresence('paypal', 'create')
        //     ->notEmpty('paypal');

        // $validator
        //     ->integer('status')
        //     ->requirePresence('status', 'create')
        //     ->notEmpty('status');

        // $validator
        //     ->dateTime('modifed')
        //     ->requirePresence('modifed', 'create')
        //     ->notEmpty('modifed');
        // $validator
        //     ->requirePresence('address_proof','create')
        //     ->notEmpty('address_proof');

        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->isUnique(['email']));

        return $rules;
    }
}
