<?php

namespace Modules\EtatStatistiques\Repository;

use Entity\Model\EtatGroupEtat;
use Entity\Model\EtatGroupUtil;
use Entity\Model\EtatGrp;
use Helpers\Outil\ArrayHelper;
use Helpers\Pagination\PaginatorGestion;
use Helpers\QueryBuilder\AbsRepository;

class EtatStatGroupRepo extends AbsRepository
{
    /**
     * @return array<mixed>
     */
    public function getDetail(string $id)
    {
        return ($entity = (new EtatGrp($this->pdo))->find($id))
            + [
                'listUtil' => $this->fetchAssocAll(
                    "SELECT HASTUTIL.* 
                    FROM HASTUTIL with(nolock)
                    JOIN ETGRPUSR with(nolock) 
                        ON ETGRPUSR.CODUSR = HASTUTIL.CODE
                        AND ETGRPUSR.CODGRP = :id",
                    [':id' => $entity['CODGRP']]
                ),
                'listEtatStat' => $this->fetchAssocAll(
                    "SELECT ETATSTAT.* 
                    FROM ETATSTAT with(nolock)
                    JOIN ETGRPET with(nolock)
                        ON ETGRPET.CODETAT = ETATSTAT.CODETAT
                        AND ETGRPET.CODGRP = :id",
                    [':id' => $entity['CODGRP']]
                ),
            ];
    }

    /**
     * @param array<mixed> $post
     * @return string[][]
     */
    public function listGroupe(array $post)
    {
        $param = [];

        $query = $this->applyFilterGestion(
            "SELECT ETATGRP.*
            FROM ETATGRP with(nolock)
            WHERE 1=1 ",
            $param,
            $post
        );

        (new PaginatorGestion())->injecterSqlCount($query, $post, $param, $this->pdo);

        $listEntity = $this->fetchAssocAll($query, $param);

        //____________________
        // Join list Utilisateur
        
        $param = [];
        $listEntity = ArrayHelper::joinManyToMany(
            $listEntity,
            $this->fetchAssocAll(
                "SELECT ETGRPUSR.CODGRP, HASTUTIL.* 
                FROM ETGRPUSR with(nolock)
                JOIN HASTUTIL with(nolock) 
                    ON HASTUTIL.CODE = ETGRPUSR.CODUSR
                    AND "
                . $this->makeWhereIn(
                    'ETGRPUSR.CODGRP',
                    array_column($listEntity, 'CODGRP'),
                    $param
                ),
                $param
            ),
            'CODGRP',
            'listUtil'
        );

        //____________________
        // Join list EtatStat

        $param = [];
        $listEntity = ArrayHelper::joinManyToMany(
            $listEntity,
            $this->fetchAssocAll(
                "SELECT ETGRPET.CODGRP, ETATSTAT.* 
                FROM ETGRPET with(nolock)
                JOIN ETATSTAT with(nolock)
                    ON ETATSTAT.CODETAT = ETGRPET.CODETAT
                    AND "
                . $this->makeWhereIn(
                    'ETGRPET.CODGRP',
                    array_column($listEntity, 'CODGRP'),
                    $param
                ),
                $param
            ),
            'CODGRP',
            'listEtatstat'
        );

        return $listEntity;
    }

    /**
     * @param array<mixed> $param
     * @param array<mixed> $post
     */
    protected function applyFilterGestion(
        string $query,
        array &$param,
        array $post
    ) :string {
        // TODO : custom filter ?

        return $this->applyFilter($query, $param, $post);
    }

//_____________________________________________________________________________
// Modifier

    /**
     * @param array<mixed> $post
     */
    public function persist(array $post) :void
    {
        $this->pdo->beginTransaction();

        $id = $post['CODGRP'];
        
        (new EtatGrp($this->pdo))->merge(ArrayHelper::filterByKey($post, [
            'CODGRP', 'LIBELLE'
        ]),);
        
        // Children
        $qbUtil = new EtatGroupUtil($this->pdo);
        $qbUtil->delete(['CODGRP' => $id]);
        foreach ($post['listAssocUtil'] as $util) {
            $qbUtil->insert(['CODGRP' => $id, 'CODUSR' => $util]);
        }
        $qbEtat = new EtatGroupEtat($this->pdo);
        $qbEtat->delete(['CODGRP' => $id]);
        foreach ($post['listAssocEtat'] as $etat) {
            $qbEtat->insert(['CODGRP' => $id, 'CODETAT' => $etat]);
        }

        $this->pdo->commit();
    }
    


    public function delete(string $id) :void
    {
        $this->pdo->beginTransaction();

        (new EtatGrp($this->pdo))->delete($id);

        (new EtatGroupUtil($this->pdo))->delete(['CODGRP' => $id]);
        (new EtatGroupEtat($this->pdo))->delete(['CODGRP' => $id]);

        $this->pdo->commit();
    }

//______________________________________________________________________________
// Sub-routine
}
