<?php

declare(strict_types=1);

namespace App\Controller;

use App\Repository\ProfileRepository;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;

final class SessionProfilesController
{
    public function __construct(
        private readonly ProfileRepository $profiles,
    ) {}

    #[Route('/api/session/profiles', name: 'api_session_profiles_list', methods: ['GET'])]
    public function list(): JsonResponse
    {
        $items = $this->profiles->createQueryBuilder('p')
            ->andWhere('p.isActive = :active')
            ->setParameter('active', true)
            ->orderBy('p.firstName', 'ASC')
            ->getQuery()
            ->getResult()
        ;

        return new JsonResponse(array_map(static function ($profile): array {
            return [
                'id'        => $profile->getId(),
                'firstName' => $profile->getFirstName(),
                'lastName'  => $profile->getLastName(),
            ];
        }, $items));
    }
}
