<?php
namespace Modules\Additional\Specifique\Client137\Modules\InfoLegal\Outil;

use API\InfoLegale\InfoLegale;
use Entity\Model\CsDon;
use Entity\Model\Dilispec;
use Entity\Model\Fourniss;
use Entity\Model\FourPartenaireData;
use Entity\Model\Memos;
use Entity\Model\PmailInt;

class API
{
    public function bash($pdo)
    {
        set_time_limit(0);
        $contentEventDistinct = '';
        $contentEventDistinctHisto = [];
        $pMailInt = new PmailInt($pdo);
        $memos = new Memos($pdo);



        $dilispec = new Dilispec($pdo);
        $codeDilispecNumFix = $dilispec->find([
            'TYPEDICO' => 'PARNCS0137',
            'CODEDICO' => 'NCS_FOURNISS_INFOLG_NOTIFIE'
        ])['LIB01DICO'] ?? '';


        
        $infoLegale = new InfoLegale($pdo);
        $csDon = new CsDon($pdo);

        $fournisseur = new Fourniss($pdo);
        $fourParamData = new FourPartenaireData($pdo);
        $fournisseurRattacher = $fourParamData->findAll(['CODEPARTENAIRE' => 'INFOLG'], [], [], null, true);

        $mailTmp = [];

        $mailContent = $pMailInt->findAll([
             'CODE_MAIL' => 'SPE0137_NOTIF_INFO'
        ]);

            
        foreach ($mailContent as $mail) {
            if (is_array($mail) && isset($mail['NUM_MEMC']) && $mail['NUM_MEMC'] != '') {
                $memoContent = $memos->find(
                    ['NODEMEMO' => $mail['NUM_MEMC']]
                );
                
                $mailTmp[$mail['CODE_LNG']] = [
                    'TX_SUJET' => $mail['TX_SUJET'],
                    'CONTMEMO' => $memoContent['CONTMEMO'],
                ];
            }
        }

        $codeEvent = [
            '5000',
            '5001',
            '5002',
            '5003',
            '5110',
            '5132',
            '5210',
            '5211',
            '5212',
            '5213',
            '5214',
            '5220',
            '5221',
            '5222',
            '5223',
            '5224',
            '5225',
            '5238',
            '5239',
            '5240'
        ];

        foreach ($fournisseurRattacher as $fournissEAT) {
            $fourniss = $fournisseur->find($fournissEAT['CODE_FOU']);
            
            $noSiret = $fourniss['NO_SIRET'];
            
            $siren = substr(str_replace(' ', '', $noSiret), 0, 9);
            
            $resultAPI = $infoLegale->companiesEvents($siren, 'FR', $codeEvent);
            $contentEventDistinct = '';
            if (is_array($resultAPI) && isset($resultAPI['count']) && $resultAPI['count']!=0) {
                $contentEventDistinctHisto = [];
                foreach ($resultAPI['events'] as $event) {
                    if (!isset($contentEventDistinctHisto[$event['eventCode']])) {
                        $contentEventDistinct .= $event['eventCode']. ' : '.$event['categorie'].'<br/>';
                        $contentEventDistinctHisto[$event['eventCode']] = true;
                    }
                }
            }
            if ($codeDilispecNumFix != '') {
                $fournissExiste = $csDon->find([
                    'NUM_FIX'=> $codeDilispecNumFix,
                    'COD_TAB' => 'FOURNISS',
                    'COD_ENR' => $fournissEAT['CODE_FOU']
                    ]);
            } else {
                $fournissExiste= [];
            }


            if ($contentEventDistinct != '') {
                if (is_array($fournissExiste) && count($fournissExiste) != 0) {
                    if ($codeDilispecNumFix != '') {
                        $csDon->update(['VALEUR' => 'O'], [
                            'NUM_FIX'=> $codeDilispecNumFix,
                            'COD_TAB' => 'FOURNISS',
                            'COD_ENR' => $fournissEAT['CODE_FOU']
                        ]);
                    }
                } else {
                    if ($codeDilispecNumFix != '') {
                        $csDon->insert([
                        'NUM_FIX'=> $codeDilispecNumFix,
                        'COD_TAB' => 'FOURNISS',
                        'COD_ENR' => $fournissEAT['CODE_FOU'],
                        'VALEUR' => 'O'
                        ]);
                    }
                    
                    \Modules\Additional\Specifique\Client137\Modules\InfoLegal\Outil\Mail::preparationEmailcopieCache($fourniss, $fournissEAT, $contentEventDistinct, $mailTmp, $pdo);
                }
            } elseif ($codeDilispecNumFix !='') {
                if (count($fournissExiste) != 0) {
                    $csDon->update(['VALEUR' => 'N'], [
                        'NUM_FIX'=> $codeDilispecNumFix,
                        'COD_TAB' => 'FOURNISS',
                        'COD_ENR' => $fournissEAT['CODE_FOU']
                    ]);
                } else {
                    $csDon->insert([
                        'NUM_FIX'=> $codeDilispecNumFix,
                        'COD_TAB' => 'FOURNISS',
                        'COD_ENR' => $fournissEAT['CODE_FOU'],
                        'VALEUR' => 'N'
                    ]);
                }
            }
        }
        return "ok";
    }
}
