<?php

namespace App\Domain\Setting\Application\QueryHandler;

use App\Domain\Setting\Application\Query\ExportContentSourceQuery;
use App\Domain\Setting\Application\Response\ExportContentSourceResponse;
use App\Domain\Setting\Domain\Contract\Repository\ContentSourceRepositoryInterface;
use DateTimeImmutable;

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

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

        $exportData = array_map(function ($contentSource) {
            return [
                'baseUrl' => $contentSource->getBaseUrl(),
                'chapterUrlFormat' => $contentSource->getChapterUrlFormat(),
                'scrapingType' => $contentSource->getScrapingType(),
                'imageSelector' => $contentSource->getImageSelector(),
                'nextPageSelector' => $contentSource->getNextPageSelector(),
                'chapterSelector' => $contentSource->getChapterSelector(),
            ];
        }, $contentSources);

        return new ExportContentSourceResponse(
            contentSources: $exportData,
            exportDate: (new DateTimeImmutable())->format('Y-m-d H:i:s')
        );
    }
}
