<?php
namespace Helpers\Securite;

use Symfony\Component\String\Slugger\AsciiSlugger;

class IntegrityCheckVerify
{

    /**
     * __construct
     *
     *@param array<mixed> $contentHash
     *@param array<mixed> $contentHashVendor
     *@param array<mixed> $errorReturn
     *
     * @return void
     */
    public function __construct(
        protected AsciiSlugger $asciiSlugger = new AsciiSlugger(),
        protected array $contentHash = [],
        protected array $contentHashVendor = [],
        protected array $errorReturn = [],
        protected string $file = __DIR__.'\\..\\..\\..\\security\\integrity\\integrity',
        protected string $fileVendor = __DIR__.'\\..\\..\\..\\security\\integrity\\integrityV'
    ) {
        $this->contentHash = json_decode(
            \Helpers\Securite\Securite::decodeChiffrageFromMagic(
                (string)file_get_contents($this->file)
            ),
            true
        );
        $this->contentHashVendor = json_decode(
            \Helpers\Securite\Securite::decodeChiffrageFromMagic(
                (string)file_get_contents($this->fileVendor)
            ),
            true
        );
    }


    public function verify(string $source, string $dirExe, bool $vendor = false) : bool
    {
        if (!is_dir($source)) {
            echo "Le dossier source n'existe pas.\n";
            return false;
        }

        $fichiers = scandir($source);
        if (is_array($fichiers)) {
            foreach ($fichiers as $fichier) {
                if ($fichier == '.'
                    || $fichier == '..'
                    || $fichier == 'robots.txt'
                    || str_starts_with($fichier, '.')
                    || str_starts_with(strtolower($fichier), 'web.config')
                ) {
                    continue;
                }

                $sourcePath = $source . DIRECTORY_SEPARATOR . $fichier;

                if (is_dir($sourcePath)) {
                    $this->verify($sourcePath, $dirExe, $vendor);
                } else {
                    $this->checkHashInFile($sourcePath, $dirExe, $vendor);
                }
            }
        }
       
        return true;
    }

    
    public function checkHashInFile(string $file, string $dirExe, bool $vendor = false) : void
    {
        $contenteHash = ($vendor == true)?$this->contentHashVendor:$this->contentHash;
       
        $fileWithoutRoot = str_replace($dirExe, '', $file);
        $key = $this->asciiSlugger->slug($fileWithoutRoot);
        if ($contenteHash[(string)$key] != hash_file('sha256', $file)) {
            $this->errorReturn[(string)$key] = $fileWithoutRoot;
        }
    }
    
    /**
     * getContentHash
     *
     * @return array<mixed>
     */
    public function getContentHash()
    {
        return $this->contentHash;
    }
    
    /**
     * getErrorReturn
     *
     * @return array<mixed>
     */
    public function getErrorReturn() : array
    {
        return $this->errorReturn;
    }

    public function isValid() : bool
    {
        return (!count($this->errorReturn))?true:false;
    }
}
