<?php
declare(strict_types=1);

namespace App\Model\Table;

use ArrayObject;
use Cake\Event\EventInterface;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class TeacherApplicationsTable extends Table
{
    public const CRAFTS = ['pottery', 'knitting', 'both'];
    public const STATUSES = ['new', 'reviewing', 'approved', 'rejected'];

    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('teacher_applications');
        $this->setDisplayField('full_name');
        $this->setPrimaryKey('id');
        $this->addBehavior('Timestamp');

        $this->belongsTo('AdminUsers', ['foreignKey' => 'reviewed_by_admin_user_id']);
        $this->belongsTo('Teachers', ['foreignKey' => 'promoted_teacher_id']);
    }

    public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options): void
    {
        $trim = ['full_name', 'email', 'phone', 'craft', 'specialties', 'portfolio_url', 'about', 'availability_summary'];
        $nullable = ['phone', 'specialties', 'portfolio_url', 'availability_summary'];
        foreach ($trim as $field) {
            if (!isset($data[$field])) continue;
            $value = trim((string)$data[$field]);
            $data[$field] = $value;
            if ($value === '' && in_array($field, $nullable, true)) {
                $data[$field] = null;
            }
        }
    }

    public function validationDefault(Validator $validator): Validator
    {
        $validator
            ->scalar('full_name')->maxLength('full_name', 150)
            ->requirePresence('full_name', 'create')
            ->notEmptyString('full_name');

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

        $validator
            ->scalar('phone')->maxLength('phone', 60)
            ->allowEmptyString('phone');

        $validator
            ->scalar('craft')
            ->inList('craft', self::CRAFTS)
            ->requirePresence('craft', 'create');

        $validator
            ->integer('years_experience')
            ->greaterThanOrEqual('years_experience', 0)
            ->lessThanOrEqual('years_experience', 80)
            ->allowEmptyString('years_experience');

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

        $validator
            ->scalar('portfolio_url')->maxLength('portfolio_url', 500)
            ->urlWithProtocol('portfolio_url')
            ->allowEmptyString('portfolio_url');

        $validator
            ->scalar('about')
            ->minLength('about', 40, 'Please tell us a little more — at least a couple of sentences.')
            ->maxLength('about', 4000)
            ->requirePresence('about', 'create')
            ->notEmptyString('about');

        $validator
            ->scalar('availability_summary')->maxLength('availability_summary', 1000)
            ->allowEmptyString('availability_summary');

        $validator
            ->scalar('status')->inList('status', self::STATUSES)
            ->allowEmptyString('status');

        return $validator;
    }
}
