<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\TicketBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
    public function getConfigTreeBuilder(): TreeBuilder
    {
        $treeBuilder = new TreeBuilder('chill_ticket');
        /** @var ArrayNodeDefinition $rootNode */
        $rootNode = $treeBuilder->getRootNode();

        /** @var ArrayNodeDefinition $ticketArray */
        $ticketArray = $rootNode
            ->children()
            ->arrayNode('ticket')
            ->addDefaultsIfNotSet()
        ;

        $ticketArray
            ->children()
            ->enumNode('person_per_ticket')
            ->values(['one', 'many'])
            ->defaultValue('many')
            ->end();

        $ticketArray
            ->children()
            ->scalarNode('response_time_exceeded_delay')
            ->info('The delay to declare a ticket as having an exceeded response time. Must be expressed as a valid parameter for instantiating a \DateInterval')
            ->defaultValue('PT12H')
            ->validate()
            ->ifTrue(function (string $value) {
                try {
                    new \DateInterval($value);
                } catch (\Throwable) {
                    return true;
                }

                return false;
            })
            ->thenInvalid('The date interval provided for response_time_exceeded_delay (%s) is invalid')
        ;

        return $treeBuilder;
    }
}
