<?php

declare(strict_types=1);

namespace App\Module\Widget\WidgetType;

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

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

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

    public static function getTypeName(): string
    {
        return 'Son Dakika Haberleri';
    }

    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.isBreaking = :isBreaking')
            ->andWhere('n.status = :status')
            ->setParameter('isBreaking', true)
            ->setParameter('status', 'published')
            ->orderBy('n.publishedAt', 'DESC')
            ->setMaxResults($limit)
            ->getQuery()
            ->getResult();
    }

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

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