<?php
declare(strict_types=1);

namespace App\Model\Table;

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

/**
 * HorariosPersonal Model
 *
 * @property \App\Model\Table\PersonalTable&\Cake\ORM\Association\BelongsTo $Personal
 *
 * @method \App\Model\Entity\HorariosPersonal newEmptyEntity()
 * @method \App\Model\Entity\HorariosPersonal newEntity(array $data, array $options = [])
 * @method array<\App\Model\Entity\HorariosPersonal> newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\HorariosPersonal get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
 * @method \App\Model\Entity\HorariosPersonal findOrCreate($search, ?callable $callback = null, array $options = [])
 * @method \App\Model\Entity\HorariosPersonal patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method array<\App\Model\Entity\HorariosPersonal> patchEntities(iterable $entities, array $data, array $options = [])
 * @method \App\Model\Entity\HorariosPersonal|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
 * @method \App\Model\Entity\HorariosPersonal saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
 * @method iterable<\App\Model\Entity\HorariosPersonal>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\HorariosPersonal>|false saveMany(iterable $entities, array $options = [])
 * @method iterable<\App\Model\Entity\HorariosPersonal>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\HorariosPersonal> saveManyOrFail(iterable $entities, array $options = [])
 * @method iterable<\App\Model\Entity\HorariosPersonal>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\HorariosPersonal>|false deleteMany(iterable $entities, array $options = [])
 * @method iterable<\App\Model\Entity\HorariosPersonal>|\Cake\Datasource\ResultSetInterface<\App\Model\Entity\HorariosPersonal> deleteManyOrFail(iterable $entities, array $options = [])
 *
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
class HorariosPersonalTable extends Table
{
    /**
     * Initialize method
     *
     * @param array<string, mixed> $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

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

        $this->addBehavior('Timestamp');

        // Relación con Personal
        $this->belongsTo('Personal', [
            'foreignKey' => 'personal_id',
            'joinType' => 'INNER', // Se asegura de que siempre haya un miembro del personal asociado
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator): Validator
    {
        $validator
            ->integer('personal_id')
            ->notEmptyString('personal_id');

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

        $validator
            ->time('hora_inicio')
            ->requirePresence('hora_inicio', 'create')
            ->notEmptyTime('hora_inicio');

        $validator
            ->time('hora_fin')
            ->requirePresence('hora_fin', 'create')
            ->notEmptyTime('hora_fin');

        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): RulesChecker
    {
        $rules->add($rules->existsIn(['personal_id'], 'Personal'), ['errorField' => 'personal_id']);

        return $rules;
    }
}
