<?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 Chill\TicketBundle\Controller\Admin\MotiveController;
use Chill\TicketBundle\Controller\MotiveApiController;
use Chill\TicketBundle\Entity\Motive;
use Chill\TicketBundle\Form\MotiveType;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\HttpFoundation\Request;

class ChillTicketExtension extends Extension implements PrependExtensionInterface
{
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = $this->getConfiguration($configs, $container);
        $config = $this->processConfiguration($configuration, $configs);

        $container->setParameter('chill_ticket', $config);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../config'));
        $loader->load('services.yaml');
    }

    public function prepend(ContainerBuilder $container)
    {
        $this->prependApi($container);
        $this->prependCruds($container);
    }

    private function prependApi(ContainerBuilder $container): void
    {
        $container->prependExtensionConfig('chill_main', [
            'apis' => [
                [
                    'class' => Motive::class,
                    'name' => 'motive',
                    'base_path' => '/api/1.0/ticket/motive',
                    'controller' => MotiveApiController::class,
                    'base_role' => 'ROLE_USER',
                    'actions' => [
                        '_index' => [
                            'methods' => [
                                Request::METHOD_GET => true,
                                Request::METHOD_HEAD => true,
                            ],
                        ],
                        '_entity' => [
                            'methods' => [
                                Request::METHOD_GET => true,
                                Request::METHOD_HEAD => true,
                            ],
                        ],
                    ],
                ],
            ],
        ]);
    }

    protected function prependCruds(ContainerBuilder $container): void
    {
        $container->prependExtensionConfig('chill_main', [
            'cruds' => [
                [
                    'class' => Motive::class,
                    'name' => 'motive',
                    'base_path' => '/admin/ticket/motive',
                    'form_class' => MotiveType::class,
                    'controller' => MotiveController::class,
                    'actions' => [
                        'index' => [
                            'template' => '@ChillTicket/Admin/Motive/index.html.twig',
                            'role' => 'ROLE_ADMIN',
                        ],
                        'new' => [
                            'role' => 'ROLE_ADMIN',
                            'template' => '@ChillTicket/Admin/Motive/new.html.twig',
                        ],
                        'view' => [
                            'role' => 'ROLE_ADMIN',
                            'template' => '@ChillTicket/Admin/Motive/view.html.twig',
                        ],
                        'edit' => [
                            'role' => 'ROLE_ADMIN',
                            'template' => '@ChillTicket/Admin/Motive/edit.html.twig',
                        ],
                    ],
                ],
            ],
        ]);
    }
}
