<?php
/**
 * AppLog Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Logging
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

namespace BplanBase\Globals\Logging;


use BplanBase\Globals\Enums\LogCode;
use Illuminate\Support\Facades\Log;
use Stringable;
use Throwable;


/**
 * AppLog Class
 *
 * @version     2.1.0 / 2026-04-16
 * @author      Emilio Cannarozzo <emilio.cannarozzo@bplan-solutions.de>
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class AppLog
{


/* +++ TRAITS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS CONSTANTS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT MEMBERS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS MEMBERS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT METHODS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS METHODS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     *
     * @param       string|Stringable|Throwable $message
     *
     * @param       string|null $prefix
     *
     * @param       LogCode|null $code
     *
     * @return      string|Stringable
     *
     * @version     5.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private static function _prefix(string|Stringable|Throwable $message, string|null $prefix, null|string $exceptionMessageIntro, LogCode|null $code = null, string|null & $traceCode = null): string|Stringable
    {
        $traceCode = 'T'.rand(10000, 99999);

        if ($message instanceof Throwable) {
            $message = $message->getMessage().' in file '.$message->getFile().' on line '.$message->getLine();

            if ($exceptionMessageIntro !== null) {
                $message = $exceptionMessageIntro.': '.$message;
            }
        }

        $codeSegment = ($code !== null) ? '['.$code->value.']' : '';

        if (empty($prefix)) {
            return '['.$traceCode.']'.$codeSegment.'> '.$message;
        }
        return '['.$traceCode.']'.$codeSegment.'['.$prefix.']> '.$message;

    } // _prefix()


    /**
     *
     * @param       array|string $context
     *
     * @return      array
     *
     * @version     1.0.0 / 2025-08-03
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    private static function _prepareContext(array|string $context): array
    {
        if (is_array($context)) {
            return $context;
        }
        return (array) trim($context);

    } // _prepareContext()


    /**
     *
     * @param       string|Stringable|Throwable $message
     *
     * @param       string|null $prefix
     *
     * @param       array|string $context
     *
     * @return      string
     *
     * @version     5.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function alert(string|Stringable|Throwable $message, string|null $prefix = null, array|string $context = [], null|string $exceptionMessageIntro = null, LogCode|null $code, string|null & $traceCode = null): string
    {
        Log::channel('applog-composite')->alert(str_repeat(' ', 4).self::_prefix($message, $prefix, $exceptionMessageIntro, $code, $traceCode), self::_prepareContext($context), $exceptionMessageIntro);

        return $message;

    } // alert()


    /**
     *
     * @param       string|Stringable|Throwable $message
     *
     * @param       string|null $prefix
     *
     * @param       array|string $context
     *
     * @return      string
     *
     * @version     5.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function critical(string|Stringable|Throwable $message, string|null $prefix = null, array|string $context = [], null|string $exceptionMessageIntro = null, LogCode|null $code = null, string|null & $traceCode = null): string
    {
        Log::channel('applog-composite')->critical(str_repeat(' ', 1).self::_prefix($message, $prefix, $exceptionMessageIntro, $code, $traceCode), self::_prepareContext($context), $exceptionMessageIntro);

        return $message;

    } // critical()


    /**
     *
     * @param       string|Stringable|Throwable $message
     *
     * @param       string|null $prefix
     *
     * @param       array|string $context
     *
     * @return      string
     *
     * @version     5.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function debug(string|Stringable|Throwable $message, string|null $prefix = null, array|string $context = [], null|string $exceptionMessageIntro = null, LogCode|null $code = null, string|null & $traceCode = null): string
    {
        Log::channel('applog')->debug(str_repeat(' ', 4).self::_prefix($message, $prefix, $exceptionMessageIntro, $code, $traceCode), self::_prepareContext($context), $exceptionMessageIntro);

        return $message;

    } // debug()


    /**
     *
     * @param       mixed $params
     *
     * @return      string
     *
     * @version     2.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function dump(mixed $params, string|null & $traceCode = null): string
    {
        $args = func_get_args();

        if (count($args) > 1) {
            $message = print_r($args, true);

        } else {
            if (is_scalar($args[0])) {
                $message = $args[0];
            } else {
                $message = print_r($args[0], true);
            }
        }
        Log::channel('applog')->info(str_repeat(' ', 5).self::_prefix($message, 'DUMP', null, $traceCode), self::_prepareContext([]), null);

        return $message;

    } // dump()


    /**
     *
     * @param       string|Stringable|Throwable $message
     *
     * @param       string|null $prefix
     *
     * @param       array|string $context
     *
     * @return      string
     *
     * @version     5.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function emergency(string|Stringable|Throwable $message, string|null $prefix = null, array|string $context = [], null|string $exceptionMessageIntro = null, LogCode|null $code = null, string|null & $traceCode = null): string
    {
        Log::channel('applog-composite')->emergency(self::_prefix($message, $prefix, $exceptionMessageIntro, $code, $traceCode), self::_prepareContext($context), $exceptionMessageIntro);

        return $message;

    } // emergency()


    /**
     *
     * @param       string|Stringable|Throwable $message
     *
     * @param       string|null $prefix
     *
     * @param       array|string $context
     *
     * @return      string
     *
     * @version     5.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function error(string|Stringable|Throwable $message, string|null $prefix = null, array|string $context = [], null|string $exceptionMessageIntro = null, LogCode|null $code = null, string|null & $traceCode = null): string
    {
        Log::channel('applog-composite')->error(str_repeat(' ', 4).self::_prefix($message, $prefix, $exceptionMessageIntro, $code, $traceCode), self::_prepareContext($context), $exceptionMessageIntro);

        return $message;

    } // error()


    /**
     *
     * @param       string|Stringable|Throwable $message
     *
     * @param       string|null $prefix
     *
     * @param       array|string $context
     *
     * @return      string
     *
     * @version     5.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function info(string|Stringable|Throwable $message, string|null $prefix = null, array|string $context = [], null|string $exceptionMessageIntro = null, LogCode|null $code = null, string|null & $traceCode = null): string
    {
        Log::channel('applog')->info(str_repeat(' ', 5).self::_prefix($message, $prefix, $exceptionMessageIntro, $code, $traceCode), self::_prepareContext($context), $exceptionMessageIntro);

        return $message;

    } // info()


    /**
     *
     * @param       string|Stringable|Throwable $message
     *
     * @param       string|null $prefix
     *
     * @param       array|string $context
     *
     * @return      string
     *
     * @version     5.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function notice(string|Stringable|Throwable $message, string|null $prefix = null, array|string $context = [], null|string $exceptionMessageIntro = null, LogCode|null $code = null, string|null & $traceCode = null): string
    {
        Log::channel('applog')->notice(str_repeat(' ', 3).self::_prefix($message, $prefix, $exceptionMessageIntro, $code, $traceCode), self::_prepareContext($context), $exceptionMessageIntro);

        return $message;

    } // notice()


    /**
     *
     * @param       string|Stringable|Throwable $message
     *
     * @param       string|null $prefix
     *
     * @param       array|string $context
     *
     * @return      string
     *
     * @version     5.0.0 / 2026-04-16
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public static function warning(string|Stringable|Throwable $message, string|null $prefix = null, array|string $context = [], null|string $exceptionMessageIntro = null, LogCode|null $code = null, string|null & $traceCode = null): string
    {
        Log::channel('applog-composite')->warning(str_repeat(' ', 2).self::_prefix($message, $prefix, $exceptionMessageIntro, $code, $traceCode), self::_prepareContext($context), $exceptionMessageIntro);

        return $message;

    } // warning()


} // class AppLog {}
