<?php
namespace App\Model\Table;

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

/**
 * Students Model
 *
 * @property \App\Model\Table\StudentCoursesTable&\Cake\ORM\Association\HasMany $StudentCourses
 * @property \App\Model\Table\StudentSessionsTable&\Cake\ORM\Association\HasMany $StudentSessions
 *
 * @method \App\Model\Entity\Student get($primaryKey, $options = [])
 * @method \App\Model\Entity\Student newEntity($data = null, array $options = [])
 * @method \App\Model\Entity\Student[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Student|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Student saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Student patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Student[] patchEntities($entities, array $data, array $options = [])
 * @method \App\Model\Entity\Student findOrCreate($search, callable $callback = null, $options = [])
 */
class StudentsTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

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

        $this->hasMany('StudentCourses', [
            'foreignKey' => 'student_id',
        ]);
        $this->hasMany('StudentSessions', [
            'foreignKey' => 'student_id',
        ]);

        $this->hasOne('RegistrationPayments', [
            'foreignKey' => 'student_id',
            'joinType' => 'LEFT',
        ]);

        $this->hasOne('PassportDetails', [
            'foreignKey' => 'student_id',
            'joinType' => 'LEFT',
        ]);
    }

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

//        $validator
//            ->scalar('reference_number')
//            ->maxLength('reference_number', 20)
//            ->requirePresence('reference_number', 'create')
//            ->notEmptyString('reference_number')
//            ->add('reference_number', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);

        $validator
            ->scalar('reference_number')
            ->maxLength('reference_number', 20)
            ->allowEmptyString('reference_number');


        $validator
            ->scalar('title')
            ->maxLength('title', 10)
            ->requirePresence('title', 'create')
            ->notEmptyString('title');

        $validator
            ->scalar('first_name')
            ->maxLength('first_name', 100)
            ->requirePresence('first_name', 'create')
            ->notEmptyString('first_name');

        $validator
            ->scalar('surname')
            ->maxLength('surname', 100)
            ->requirePresence('surname', 'create')
            ->notEmptyString('surname');

        $validator
            ->date('date_of_birth')
            ->requirePresence('date_of_birth', 'create')
            ->notEmptyDate('date_of_birth')
            ->add('date_of_birth', 'minAge', [
                'rule' => function ($value, $context) {
                    $dob = new FrozenDate($value);
                    $today = FrozenDate::today();
                    $tenYearsAgo = $today->modify('-10 years');
                    return $dob <= $tenYearsAgo;
                },
                'message' => 'You must be at least 10 years old.'
            ]);;

        $validator
            ->date('last_join_date')
            ->allowEmptyString('last_join_date');

      $validator
          ->integer('mod_of_study');

        $validator
            ->scalar('birth_place')
            ->maxLength('birth_place', 100)
            ->requirePresence('birth_place', 'create')
            ->notEmptyString('birth_place');

        $validator
            ->scalar('gender')
            ->maxLength('gender', 10)
            ->requirePresence('gender', 'create')
            ->notEmptyString('gender');

        $validator
            ->scalar('nationality')
            ->maxLength('nationality', 100)
            ->requirePresence('nationality', 'create')
            ->notEmptyString('nationality');

        $validator
            ->scalar('passport_number')
            ->maxLength('passport_number', 20)
            ->requirePresence('passport_number', 'create')
            ->notEmptyString('passport_number');

        $validator
            ->email('email', false, 'Please enter a valid email address')
            ->requirePresence('email', 'create','Email is required')
            ->notEmptyString('email','Email cannot be empty')
            ->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table','message' => 'This email is already registered']);

        $validator
            ->scalar('phone_number')
            ->maxLength('phone_number', 20)
            ->requirePresence('phone_number', 'create')
            ->notEmptyString('phone_number');

        $validator
            ->scalar('address1')
            ->maxLength('address1', 255)
            ->requirePresence('address1', 'create')
            ->notEmptyString('address1');

        $validator
            ->scalar('address2')
            ->maxLength('address2', 255)
            ->allowEmptyString('address2');

        $validator
            ->scalar('city')
            ->maxLength('city', 50)
            ->requirePresence('city', 'create')
            ->notEmptyString('city');

        $validator
            ->scalar('state')
            ->maxLength('state', 50)
            ->allowEmptyString('state');

        $validator
            ->scalar('zip_code')
            ->maxLength('zip_code', 10)
            ->requirePresence('zip_code', 'create')
            ->notEmptyString('zip_code');

        $validator
            ->scalar('country')
            ->maxLength('country', 100)
            ->requirePresence('country', 'create')
            ->notEmptyString('country');

        $validator
            ->scalar('emergency_contact_name')
            ->maxLength('emergency_contact_name', 150)
            ->requirePresence('emergency_contact_name', 'create')
            ->notEmptyString('emergency_contact_name');

        $validator
            ->scalar('emergency_contact_relation')
            ->maxLength('emergency_contact_relation', 20)
            ->requirePresence('emergency_contact_relation', 'create')
            ->notEmptyString('emergency_contact_relation');

        $validator
            ->scalar('emergency_contact_email')
            ->maxLength('emergency_contact_email', 100)
            ->requirePresence('emergency_contact_email', 'create')
            ->notEmptyString('emergency_contact_email');

        $validator
            ->scalar('emergency_contact_phone_number')
            ->maxLength('emergency_contact_phone_number', 20)
            ->requirePresence('emergency_contact_phone_number', 'create')
            ->notEmptyString('emergency_contact_phone_number');

        $validator
            ->scalar('speak_language')
            ->maxLength('speak_language', 100)
            ->requirePresence('speak_language', 'create')
            ->notEmptyString('speak_language');

        $validator
            ->scalar('education_name1')
            ->maxLength('education_name1', 150)
            ->requirePresence('education_name1', 'create')
            ->notEmptyString('education_name1');

        $validator
            ->scalar('education_year1')
            ->maxLength('education_year1', 150)
            ->requirePresence('education_year1', 'create')
            ->notEmptyString('education_year1');

        $validator
            ->scalar('education_institution1')
            ->maxLength('education_institution1', 255)
            ->requirePresence('education_institution1', 'create')
            ->notEmptyString('education_institution1');

        $validator
            ->scalar('education_name2')
            ->maxLength('education_name2', 150)
            ->allowEmptyString('education_name2');

        $validator
            ->scalar('education_year2')
            ->maxLength('education_year2', 150)
            ->allowEmptyString('education_year2');

        $validator
            ->scalar('education_institution2')
            ->maxLength('education_institution2', 255)
            ->allowEmptyString('education_institution2');

        $validator
            ->scalar('education_name3')
            ->maxLength('education_name3', 150)
            ->allowEmptyString('education_name3');

        $validator
            ->scalar('education_year3')
            ->maxLength('education_year3', 50)
            ->allowEmptyString('education_year3');

        $validator
            ->scalar('education_institution3')
            ->maxLength('education_institution3', 255)
            ->allowEmptyString('education_institution3');

        $validator
            ->scalar('employer_name')
            ->maxLength('employer_name', 150)
            ->allowEmptyString('employer_name');

        $validator
            ->scalar('position')
            ->maxLength('position', 100)
            ->allowEmptyString('position');

        $validator
            ->integer('status')
            ->allowEmptyString('status');

        $validator
            ->integer('current_checklist_id')
            ->allowEmptyString('current_checklist_id');

        $validator
            ->integer('referring_agent_id')
            ->allowEmptyString('referring_agent_id');

        $validator
            ->scalar('referring_agent_name')
            ->maxLength('referring_agent_name', 150)
            ->allowEmptyString('referring_agent_name');


        $validator
            ->integer('created_by')
            ->allowEmptyString('created_by');

        $validator
            ->integer('updated_by')
            ->allowEmptyString('updated_by');

        $validator
            ->dateTime('created_at')
            ->allowEmptyDateTime('created_at');

        $validator
            ->dateTime('updated_at')
            ->allowEmptyDateTime('updated_at');

        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']));
        $rules->add($rules->isUnique(['reference_number']));

        return $rules;
    }


}
