<?php

namespace App\Domain\Setting\Application\QueryHandler;

use App\Domain\Setting\Application\Query\ListContentSourceQuery;
use App\Domain\Setting\Application\Response\ContentSourceListResponse;
use App\Domain\Setting\Application\Response\ContentSourceResponse;
use App\Domain\Setting\Domain\Contract\Repository\ContentSourceRepositoryInterface;

readonly class ListContentSourceQueryHandler
{
    public function __construct(
        private ContentSourceRepositoryInterface $contentSourceRepository
    ) {
    }

    public function handle(ListContentSourceQuery $query): ContentSourceListResponse
    {
        $contentSources = $this->contentSourceRepository->findAll();

        $responses = array_map(
            fn ($contentSource) => ContentSourceResponse::fromDomain($contentSource),
            $contentSources
        );

        return new ContentSourceListResponse(
            contentSources: $responses,
            total: count($responses)
        );
    }
}
