<?php
namespace App\Model\Table;

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

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

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

        $this->hasMany('StudentCourses', [
            'foreignKey' => 'course_id',
        ]);
        $this->belongsTo('CourseCategories', [
            'foreignKey' => 'course_category_id',
            'joinType' => 'left'
        ]);
        $this->belongsTo('QualificationTypes', [
            'foreignKey' => 'qualification_type_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('name')
            ->maxLength('name', 100)
            ->requirePresence('name', 'create')
            ->notEmptyString('name');


        $validator
            ->integer('course_category_id')
            ->requirePresence('course_category_id', 'create')
            ->notEmptyString('course_category_id');

        $validator
            ->integer('qualification_type_id')
            ->requirePresence('qualification_type_id', 'create')
            ->notEmptyString('qualification_type_id');

        $validator
            ->integer('level')
            ->requirePresence('level', 'create')
            ->notEmptyString('level');

        $validator
            ->decimal('total_credit')
            ->requirePresence('total_credit', 'create')
            ->notEmptyString('total_credit');

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

        $validator
            ->scalar('progression_routes')
            ->allowEmptyString('progression_routes');

        $validator
            ->integer('availability_type')
            ->requirePresence('availability_type', 'create')
            ->notEmptyString('availability_type');

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

        $validator
            ->integer('accreditation_status')
            ->requirePresence('accreditation_status', 'create')
            ->notEmptyString('accreditation_status');

        $validator
            ->scalar('centre_specification_link')
            ->maxLength('centre_specification_link', 200)
            ->allowEmptyString('centre_specification_link');

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

        $validator
            ->integer('delivery_mode')
            ->requirePresence('delivery_mode', 'create')
            ->notEmptyString('delivery_mode');

        $validator
            ->integer('duration')
            ->requirePresence('duration', 'create')
            ->notEmptyString('duration');

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

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

        $validator
            ->allowEmptyString('status');

        $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;
    }
}
