<?php

namespace App\Repository;

use App\Entity\Indicator;
use App\Entity\Indice;
use App\Entity\Target;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\Query\Expr;

/**
 * @method Indicator|null find($id, $lockMode = null, $lockVersion = null)
 * @method Indicator|null findOneBy(array $criteria, array $orderBy = null)
 * @method Indicator[]    findAll()
 * @method Indicator[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class IndicatorRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Indicator::class);
    }

    public function findByAgenda($agenda)
    {
        return $this->createQueryBuilder('i')
            ->join('i.target', 't')
            ->join('t.goal', 'g')
            ->where('g.agenda = :agenda')
            ->andWhere('i.communal = :communal')
            ->setParameter('agenda', $agenda)
            ->setParameter('communal', false)
            ->orderBy('i.code', 'ASC')
            ->getQuery()
            ->getResult()
        ;
    }

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

    /*
    public function findOneBySomeField($value): ?Indicator
    {
        return $this->createQueryBuilder('i')
            ->andWhere('i.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
    
    public function getIndice($year, Target $target)
    {
        /*$whereClause = $action ? 'a.id = :action' : ($programm ? 'p.id = :programm' : 'm.id = :ministry');
        $paramClause = $action ? 'action' : ($programm ? 'programm' : 'ministry');
        $paramObject = $action ? $action : ($programm ? $programm : $ministry);

        $targetClause = $flag ? 'n.target is null' : '1=1';*/

        $qb = $this->createQueryBuilder('i')
            ->select('i.code')
            ->addSelect('i.name')
            //->addSelect('IDENTITY(n.target) as target')
            ->addSelect('n.value')
            ->addSelect('i.maxValue')
            ->addSelect('i.minValue')
            //->addSelect('y.amount AS amount')

            ->innerJoin(Indice::class, 'n', Expr\Join::WITH, 'i.id = n.indicator')

            ->andWhere('n.year = :year')
            ->andWhere('i.target = :target')

            ->setParameter('year', $year)
            ->setParameter('target', $target)
            ;

        $rows = $qb->getQuery()->getResult();

        foreach ($rows as $key => $row) {
            /*$indicator = $this->getEntityManager()
                ->getRepository(Indicator::class)
                ->findOneBy(['id' => $rows[$key]['indicator']]);
            $rows[$key]['name'] = $indicator->getName();*/
            //$rows[$key]['action'] = $activity->getAction()->getName();
            //$rows[$key]['programm'] = $activity->getAction()->getProgramm()->getName();

            /*$nature = $this->getEntityManager()
                ->getRepository(Nature::class)
                ->findOneBy(['id' => $rows[$key]['nature']]);
            $rows[$key]['nature'] = $nature->getName();
            $target = $this->getEntityManager()
                ->getRepository(Target::class)
                ->findOneBy(['id' => $rows[$key]['target']]);
            if ($target) {
                $rows[$key]['target'] = $target->getName();
            }
            $appreciation = $this->getEntityManager()
                ->getRepository(Appreciation::class)
                ->findOneBy(['id' => $rows[$key]['appreciation']]);
            $rows[$key]['appreciation'] = $appreciation->getName();
            $rows[$key]['flag'] = $appreciation->getFlag();*/
        }

        return $rows;
    } 
}
