<?php

namespace App\UrlGenerator;

use App\Core\Entity\Site\Node;
use App\Entity\Blog\Category;
use App\Repository\Blog\CategoryRepositoryQuery;
use App\Repository\Blog\PostRepositoryQuery;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
 * class PostGenerator.
 *
 * @author Simon Vieille <simon@deblan.fr>
 */
class PostGenerator
{
    protected PostRepositoryQuery $postQuery;
    protected CategoryRepositoryQuery $categoryQuery;
    protected UrlGeneratorInterface $urlGenerator;

    public function __construct(
        PostRepositoryQuery $postQuery,
        CategoryRepositoryQuery $categoryQuery,
        UrlGeneratorInterface $urlGenerator
    ) {
        $this->postQuery = $postQuery;
        $this->categoryQuery = $categoryQuery;
        $this->urlGenerator = $urlGenerator;
    }

    public function post(Node $node, array $options): array
    {
        $entities = $this->postQuery->create()
            ->published()
            ->find()
        ;

        $urls = [];

        foreach ($entities as $entity) {
            $urls[] = $this->urlGenerator->generate(
                $node->getRouteName(),
                [
                    'post' => $entity->getId(),
                    'slug' => $entity->getSlug(),
                    '_domain' => $options['_domain'],
                ],
                UrlGeneratorInterface::ABSOLUTE_URL
            );
        }

        return $urls;
    }

    public function category(): array
    {
        $entities = $this->categoryQuery->create()
            ->andWhere('.isActive = 1')
            ->find()
        ;

        $urls = [];

        foreach ($entities as $entity) {
            $urls[] = $this->urlGenerator->generate(
                'blog_menu_category',
                [
                    'category' => $entity->getId(),
                    'slug' => $entity->getSlug(),
                ],
                UrlGeneratorInterface::ABSOLUTE_URL
            );
        }

        return $urls;
    }
}
