<?php

namespace App\Repository;

use App\Entity\User;
use App\Entity\Broadcast;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Broadcast>
 */
class BroadcastRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Broadcast::class);
    }

    /**
     * @return Broadcast[] Returns an array of Broadcast objects
     */
    public function findByUser(User $user): array
    {
        return $this->createQueryBuilder('b')
            ->where('b.create_by = :User')
            ->setParameter('User', $user)
            ->orderBy('b.date_create', 'DESC')
            ->getQuery()
            ->getResult()
        ;
    }

    /**
     * @return Broadcast[] Returns an array of Broadcast objects
     */
    public function findBroadcast()
    {
        return $this->createQueryBuilder('b')
            ->where('b.last_state = 31')
            ->andWhere('b.date_start <= :DateNow')
            ->andWhere('b.date_end >= :DateNow')
            ->setParameter('DateNow', new \DateTime())
            ->getQuery()
            ->getResult()
        ;
    }

    /**
     * @return Broadcast[] Returns an array of Broadcast objects
     */
    public function findBroadcastByUser(User $user)
    {
        return $this->createQueryBuilder('b')
            ->where('b.last_state = 32')
            ->where('b.create_by = :User')
            ->setParameter('User', $user)
            ->andWhere('b.date_start <= :DateNow')
            ->setParameter('DateNow', new \DateTime())
            ->getQuery()
            ->getResult()
        ;
    }
}
