<?php

declare(strict_types=1);

namespace App\Module\Widget\WidgetType;

use App\Entity\Tenant\News;
use Doctrine\ORM\EntityManagerInterface;

class HeadlineSliderWidget implements WidgetTypeInterface
{
    public function __construct(
        private readonly EntityManagerInterface $entityManager,
    ) {}

    public static function getTypeSlug(): string
    {
        return 'headline_slider';
    }

    public static function getTypeName(): string
    {
        return 'Manset Slider';
    }

    public static function getConfigSchema(): array
    {
        return [
            [
                'name' => 'limit',
                'type' => 'number',
                'default' => 5,
                'label' => 'Haber Sayisi',
            ],
        ];
    }

    public function getData(array $config): array
    {
        $limit = (int) ($config['limit'] ?? 5);

        return $this->entityManager->getRepository(News::class)
            ->createQueryBuilder('n')
            ->where('n.isFeatured = :isFeatured OR n.isHeadline = :isHeadline')
            ->andWhere('n.status = :status')
            ->setParameter('isFeatured', true)
            ->setParameter('isHeadline', true)
            ->setParameter('status', 'published')
            ->orderBy('n.publishedAt', 'DESC')
            ->setMaxResults($limit)
            ->getQuery()
            ->getResult();
    }

    public function getTemplate(): string
    {
        return 'widget/headline_slider.html.twig';
    }

    public static function getAllowedZones(): array
    {
        return ['homepage'];
    }
}
