<?php

namespace App\Repository;

use App\Entity\Appreciation;
use App\Entity\Notation;
use App\Entity\Activity;
use App\Entity\Action;
use App\Entity\Programm;
use App\Entity\Ministry;
use App\Entity\Commune;
use App\Entity\Goal;
use App\Entity\Target;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\Expr;
use Doctrine\Persistence\ManagerRegistry;

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

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

    public function getSensitivityByMinistry($year, Ministry $ministry, ?Programm $programm, ?Action $action)
    {
        $whereClause = $action ? 'a.id = :action' : ($programm ? 'p.id = :programm' : 'm.id = :ministry');
        $paramClause = $action ? 'action' : ($programm ? 'programm' : 'ministry');
        $paramObject = $action ? $action : ($programm ? $programm : $ministry);

        $qb = $this->createQueryBuilder('t')
            ->select('t.code')
            ->addSelect('t.name')
            ->addSelect('IDENTITY(t.goal) AS goal')
            ->addSelect('SUM(n.sensitivity) AS sumSensitivity')
            ->addSelect('COUNT(n.activity) AS nbActivity')
            ->addSelect('SUM(CASE WHEN n.duplicated=false THEN y.amount ELSE 0 END) AS amount')

            ->innerJoin(Notation::class, 'n', Expr\Join::WITH, 't.id = n.target')
            ->innerJoin(Activity::class, 'y', Expr\Join::WITH, 'y.id = n.activity')
            ->innerJoin(Action::class, 'a', Expr\Join::WITH, 'a.id = y.action')
            ->innerJoin(Programm::class, 'p', Expr\Join::WITH, 'p.id = a.programm')
            ->innerJoin(Ministry::class, 'm', Expr\Join::WITH, 'm.id = p.ministry')

            ->andWhere('n.sensitivity != 0')
            ->andWhere('a.year = :year')
            ->andWhere($whereClause)

            ->setParameter('year', $year)
            ->setParameter($paramClause, $paramObject)
            ->groupBy('t.id') 
            ->orderBy('t.id', 'ASC')
            ;

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

        foreach ($rows as $key => $row) {
            $rows[$key]['name'] = $rows[$key]['code'] . ' - ' . $rows[$key]['name']; 
            $rows[$key]['code'] = $this->getEntityManager()
                ->getRepository(Goal::class)
                ->find($rows[$key]['goal'])->getCode();

            $rows[$key]['sensitivity'] = $rows[$key]['nbActivity'] ? round($rows[$key]['sumSensitivity'] / $rows[$key]['nbActivity'], 2) : 0;
            $appreciation = $this->getEntityManager()
                ->getRepository(Appreciation::class)
                ->findOneByValue($rows[$key]['sensitivity']);
            $rows[$key]['appreciation'] = $appreciation->getName();
            $rows[$key]['flag'] = $appreciation->getFlag();
        }

        return $rows;
    }

    public function getSensitivityByCommune($year, Commune $commune, ?Programm $programm, ?Action $action)
    {
        $whereClause = $action ? 'a.id = :action' : ($programm ? 'p.id = :programm' : 'm.id = :commune');
        $paramClause = $action ? 'action' : ($programm ? 'programm' : 'commune');
        $paramObject = $action ? $action : ($programm ? $programm : $commune);

        $qb = $this->createQueryBuilder('t')
            ->select('t.code')
            ->addSelect('t.name')
            ->addSelect('IDENTITY(t.goal) AS goal')
            ->addSelect('SUM(n.sensitivity) AS sumSensitivity')
            ->addSelect('COUNT(n.activity) AS nbActivity')
            ->addSelect('SUM(CASE WHEN n.duplicated=false THEN y.amount ELSE 0 END) AS amount')

            ->innerJoin(Notation::class, 'n', Expr\Join::WITH, 't.id = n.target')
            ->innerJoin(Activity::class, 'y', Expr\Join::WITH, 'y.id = n.activity')
            ->innerJoin(Action::class, 'a', Expr\Join::WITH, 'a.id = y.action')
            ->innerJoin(Programm::class, 'p', Expr\Join::WITH, 'p.id = a.programm')
            ->innerJoin(Commune::class, 'm', Expr\Join::WITH, 'm.id = p.commune')

            ->andWhere('n.sensitivity != 0')
            ->andWhere('a.year = :year')
            ->andWhere($whereClause)

            ->setParameter('year', $year)
            ->setParameter($paramClause, $paramObject)
            ->groupBy('t.id') 
            ->orderBy('t.id', 'ASC')
            ;

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

        foreach ($rows as $key => $row) {
            $rows[$key]['name'] = $rows[$key]['code'] . ' - ' . $rows[$key]['name']; 
            $rows[$key]['code'] = $this->getEntityManager()
                ->getRepository(Goal::class)
                ->find($rows[$key]['goal'])->getCode();

            $rows[$key]['sensitivity'] = $rows[$key]['nbActivity'] ? round($rows[$key]['sumSensitivity'] / $rows[$key]['nbActivity'], 2) : 0;
            $appreciation = $this->getEntityManager()
                ->getRepository(Appreciation::class)
                ->findOneByValue($rows[$key]['sensitivity']);
            $rows[$key]['appreciation'] = $appreciation->getName();
            $rows[$key]['flag'] = $appreciation->getFlag();
        }

        return $rows;
    }
}
