<?php

namespace App\Repository;

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

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

    public function reverseNoteSort(?User $user): array
    {
        if (!$user) {
            return [];
        }

        $ret = $this->createQueryBuilder('n')
            ->where('n.user = :user')
            ->setParameter('user', $user->getId(), 'uuid')
            ->orderBy('n.date', 'DESC')
            ->getQuery()
            ->getResult();

        return $ret;
    }

    public function getLast4Notes(?User $user): array
    {
        if (!$user) {
            return [];
        }
        $ret = $this->createQueryBuilder('n')
            ->where('n.user = :user')
            ->setParameter('user', $user->getId(), 'uuid')
            ->orderBy('n.date', 'DESC')
            ->setMaxResults(4)
            ->getQuery()
            ->getResult();

        return $ret;
    }

    public function findNote(string $query, User $user): array
    {
        $ret = $this->createQueryBuilder('n')
            ->where('n.title LIKE :query')
            ->orWhere('n.passage LIKE :query')
            ->orWhere('n.text LIKE :query')
            ->andWhere('n.user = :user')
            ->setParameter('query', "%{$query}%")
            ->setParameter('user', $user->getId(), 'uuid')
            ->orderBy('n.date', 'DESC')
            ->setMaxResults(3)
            ->getQuery()
            ->getResult();

        return $ret;
    }

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

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