<?php

namespace App\Repository;

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

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

    public function searchAnnouncements(?string $companyName, ?string $location, ?string $category): array
    {
        $queryBuilder = $this->createQueryBuilder('a')->InnerJoin('a.company', 'c');

        if ($companyName) {
            $queryBuilder->andWhere('c.name LIKE :companyName')
                ->setParameter('companyName', '%' . $companyName . '%');
        }

        if ($location) {
            $queryBuilder->andWhere('c.address LIKE :location')
                ->setParameter('location', '%' . $location . '%');
        }

        if ($category) {
            $queryBuilder->andWhere('a.title LIKE :category OR a.description LIKE :category')
                ->setParameter('category', '%' . $category . '%');
        }

        return $queryBuilder->getQuery()->getResult();
    }

    //    /**
    //     * @return Announcement[] Returns an array of Announcement objects
    //     */
    //    public function findByExampleField($value): array
    //    {
    //        return $this->createQueryBuilder('a')
    //            ->andWhere('a.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->orderBy('a.id', 'ASC')
    //            ->setMaxResults(10)
    //            ->getQuery()
    //            ->getResult()
    //        ;
    //    }

    //    public function findOneBySomeField($value): ?Announcement
    //    {
    //        return $this->createQueryBuilder('a')
    //            ->andWhere('a.exampleField = :val')
    //            ->setParameter('val', $value)
    //            ->getQuery()
    //            ->getOneOrNullResult()
    //        ;
    //    }
}
