<?php

namespace Modules\Suivi\Service;

use Helpers\Outil\Sql;
use Helpers\Trad\ITrad;

class RuptureService
{

    /**
     * @var \PDO $pdo
     */
    public $pdo;

    public function __construct(\pdo $pdo)
    {
        $this->pdo = $pdo;
    }

    /**
     * @param array<mixed> $post
     * @param array<mixed> $rupture
     * @return void
     */
    public function cleanPostForRupture(&$post, $rupture, string $rowTri = '') : void
    {
        if (isset($post['rupture-1'])  && $post['rupture-1']!= '') {
            // si une rupture est activé, alors les TRI des tableau sont désactivé, la rupture a priorité.
            foreach ($post as $k => $v) {
                if (strpos($k, 'tri_') === 0) {
                    unset($post[$k]);
                }
            }

            if ($rupture) {
                foreach ($rupture as $k => $v) {
                    $post['tri_'.$k] = 'ASC';
                }
            } else {
                $post['tri_'.$post['rupture-1']] = 'ASC';

                if (isset($post['rupture-2'])  && $post['rupture-2']!= '') {
                    $post['tri_'.$post['rupture-2']] = 'ASC';
                }

                if ($rowTri) {
                    $post['tri_'.$rowTri] = 'DESC';
                }
            }
        }
    }

    /**
     * @param array<mixed> $liste
     * @param array<mixed> $post
     * @return array<mixed>
     */
    public function getListeRupture(&$liste, $post)
    {
        $rupture1 = $post['rupture-1']??null;
        $rupture2 = $post['rupture-2']??null;

        if (!$rupture1) {
            return [];
        }

        $ruptureExplod1 = explode('.', $rupture1);
        $ruptureExplod2 = explode('.', $rupture2);

        if ($ruptureExplod1[1]??null) {
            $keyRupture1 = $ruptureExplod1[1];
        } else {
            $keyRupture1 = $rupture1;
        }

        if ($ruptureExplod2[1]??null) {
            $keyRupture2 = $ruptureExplod2[1];
        } else {
            $keyRupture2 = $rupture2;
        }

        $searchRupture = [];
        foreach ($liste as &$val) {
            //rupture 1
            $ligneRupture = [
                $rupture1 =>  $val[$keyRupture1]??null,
            ];

            $keyRupture = implode('-', $ligneRupture);
            $searchRupture[$keyRupture] = $ligneRupture;
            
            $val['KEY_RUPTURE1'] = $keyRupture;

            //rupture 2
            if ($rupture2) {
                $ligneRupture = [
                    $rupture1 =>  $val[$keyRupture1]??null,
                    $rupture2 =>  $val[$keyRupture2]??null,
                ];
    
                $keyRupture = implode('-', $ligneRupture);
                $searchRupture[$keyRupture] = $ligneRupture;
                
                $val['KEY_RUPTURE2'] = $keyRupture;
            }
        }

        return $searchRupture;
    }

    /**
     * Undocumented function
     *
     * @param string $req
     * @param array<mixed> $rupture
     * @return string
     */
    public function genrateRequeteRupture(string $req, $rupture, string $clauseUO = '')
    {
        $groupBy = [];
        foreach ($rupture as $k => $r) {
            $groupBy[] = explode('.', $k)[1];
        }

        $req = $clauseUO."select SUM(RUPTURE) RUPTURE_TOTAL from ($req) rupture group by ".implode(',', $groupBy);

        return $req;
    }
}
