<?php
namespace App\Model\Table;


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

/**
 * CheckLists Model

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

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

    }

    /**
     * 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')->add('name', 'unique', [
                'rule' => 'validateUnique',
                'provider' => 'table',
                'message' => 'This name is already taken.'
            ]);;

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

        $validator
            ->integer('sequence')
            ->notEmptyString('sequence')->add('sequence', 'unique', [
                'rule' => 'validateUnique',
                'provider' => 'table',
                'message' => 'This sequence is already taken.'
            ]);;

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

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

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

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

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

        return $validator;
    }

    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->isUnique(['name']), [
            'errorField' => 'name',
            'message' => 'name is already in use'
        ]);

        $rules->add($rules->isUnique(['sequence']), [
            'errorField' => 'sequence',
            'message' => 'sequence is already in use'
        ]);

        return $rules;
    }
}
