<?php
/**
 * COmanage Match Endpoints Table
 *
 * Portions licensed to the University Corporation for Advanced Internet
 * Development, Inc. ("UCAID") under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.
 *
 * UCAID licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @link          https://www.internet2.edu/comanage COmanage Project
 * @package       match
 * @since         COmanage Match v1.1.0
 * @license       Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 */

declare(strict_types = 1);

namespace App\Model\Table;

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

//use \App\Lib\Enum\ResolutionModeEnum;

class EndpointsTable extends Table {
  use \App\Lib\Traits\AutoViewVarsTrait;
  use \App\Lib\Traits\MatchgridLinkTrait;
  use \App\Lib\Traits\PrimaryLinkTrait;
  
  /**
   * Perform Cake Model initialization.
   *
   * @since  COmanage Match v1.1.0
   * @param  array  $config Configuration options passed to constructor
   */
  
  public function initialize(array $config): void {
    $this->addBehavior('Timestamp');
    
    // Define associations
    $this->hasMany('MatchgridSettings')
         ->setForeignKey('resolution_notification_endpoint_id')
         ->setProperty('resolution_notification_endpoint');
    
    $this->belongsTo('Matchgrids');
    
    $this->setDisplayField('description');
    
    $this->setPrimaryLink('matchgrid_id');
    $this->setRequiresMatchgrid(true);
  }
  
  /**
   * Set validation rules.
   * 
   * @since  COmanage Match v1.1.0
   * @param  Validator $validator Validator
   * @return \Cake\Validation\Validator Validator
   */
  
  public function validationDefault(Validator $validator): Validator {
    $validator->add(
      'matchgrid_id',
      'content',
      [ 'rule' => 'isInteger' ]
    );
    $validator->notBlank('matchgrid_id');

    $validator->add(
      'description',
      'length',
      [ 'rule' => [ 'maxLength', 128 ],
        'message' => __('match.er.val.length', [128]) ]
    );
    $validator->allowEmptyString('description');

    $validator->add(
      'url',
      'url',
      [ 'rule' => [ 'url', true ]]
    );

    $validator->add(
      'url',
      'length',
      [ 'rule' => [ 'maxLength', 256 ],
        'message' => __('match.er.val.length', [256]) ]
    );
    $validator->notEmptyString('url');
    
    $validator->add(
      'username',
      'length',
      [ 'rule' => [ 'maxLength', 128 ],
        'message' => __('match.er.val.length', [128]) ]
    );
    $validator->allowEmptyString('username');

    $validator->add(
      'password',
      'length',
      [ 'rule' => [ 'maxLength', 256 ],
        'message' => __('match.er.val.length', [256]) ]
    );
    $validator->allowEmptyString('password');
    
    return $validator; 
  }
}