<?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\Controller;

use Chill\DocStoreBundle\Serializer\Normalizer\StoredObjectNormalizer;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\TicketBundle\Serializer\Normalizer\MotiveNormalizer;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\Request;

final class MotiveApiController extends ApiController
{
    protected function customizeQuery(string $action, Request $request, $query): void
    {
        /* @var $query QueryBuilder */
        $query->andWhere('e.active = TRUE')->andWhere('e.parent IS NULL');
    }

    protected function getContextForSerialization(string $action, Request $request, string $_format, $entity): array
    {
        return match ($request->getMethod()) {
            Request::METHOD_GET => ['groups' => ['read', 'read:extended', StoredObjectNormalizer::DOWNLOAD_LINK_ONLY, MotiveNormalizer::GROUP_PARENT_TO_CHILDREN]],
            default => parent::getContextForSerialization($action, $request, $_format, $entity),
        };
    }
}
