<?php

namespace Helpers\Log;

use Helpers\Session\ParseBase1;

class LogSysteme
{

    private string $fileName;
    private string $directory;
    private string $date;
    private string $hour;
   
   
    public function __construct(int $contexte = 1)
    {
        $this->date = '*********************   Date: ' . date('d/m/Y') . '  ********************';
        $this->hour = date('H:i:s');

        switch ($contexte) {
            case 1:
                $this->fileName = 'GENERALERRORLOG_OXAPROC.log';
                $this->directory = (new ParseBase1())->getRep()->log;
                break;
            
            case 2:
                $this->fileName = self::getServerName().'.stats_SQLServer.log';
                $this->directory = (new ParseBase1())->getRep()->tmp;
                break;
            
            case 3:
                $this->fileName = self::getServerName().'.req_SQLServer.log';
                $this->directory = (new ParseBase1())->getRep()->tmp;
                break;
            case 4:
                $this->fileName = date('Ymd').'_req_SQLServer.log';
                $this->directory = (new ParseBase1())->getRep()->log;
        }
    }

    public function setContentInLog(string $content) : int
    {
        return (int) file_put_contents(
            $this->directory.$this->fileName,
            $this->date.PHP_EOL.$this->hour. ' : '.$content.PHP_EOL,
            FILE_APPEND
        );
    }

    /**
     * Permet de préfixer error_log avec PHP_DEV_ERROR
     * Important pour les reports Grafana, a utiliser dans Oxaproc
     * pour chaque error_log à implémenter
     *
     * @param string $message
     */
    public static function prefixErrorLog(string $message): bool
    {
        return error_log('PHP_DEV_ERROR : ' . $message);
    }

    public static function getServerName(): string
    {
        return 'PHP_'.$_SERVER['COMPUTERNAME'];
    }
}
