<?php

/*
--------------------------------------------------
System Error Handler
--------------------------------------------------
*/

if (!function_exists('ec_register_error_handlers')) {

    function ec_register_error_handlers(): void
    {
        /*
        Error handler
        */

        set_error_handler(function (
            $severity,
            $message,
            $file,
            $line
        ) {

            ec_log_error(
                'PHP_ERROR',
                $message,
                $file,
                $line
            );

            return false;

        });

        /*
        Exception handler
        */

        set_exception_handler(function ($exception) {

            ec_log_error(
                'EXCEPTION',
                $exception->getMessage(),
                $exception->getFile(),
                $exception->getLine()
            );

            ec_render_error_screen(
                'System Error',
                $exception->getMessage()
            );

        });

        /*
        Shutdown handler
        */

        register_shutdown_function(function () {

            $error = error_get_last();

            if ($error) {

                ec_log_error(
                    'FATAL',
                    $error['message'],
                    $error['file'],
                    $error['line']
                );

                ec_render_error_screen(
                    'Fatal Error',
                    $error['message']
                );

            }

        });

    }

}

/*
--------------------------------------------------
Logger
--------------------------------------------------
*/

if (!function_exists('ec_log_error')) {

    function ec_log_error(
        string $type,
        string $message,
        string $file,
        int $line
    ): void {

        $log =
            '[' . date('Y-m-d H:i:s') . '] '
            . $type . ': '
            . $message
            . ' in '
            . $file
            . ':'
            . $line;

        error_log($log);

    }

}

/*
--------------------------------------------------
Friendly screen
--------------------------------------------------
*/

if (!function_exists('ec_render_error_screen')) {

    function ec_render_error_screen(
        string $title,
        string $message
    ): void {

        if (headers_sent() === false) {

            http_response_code(500);

        }

        echo '<html>';
        echo '<head>';
        echo '<title>' . htmlspecialchars($title) . '</title>';
        echo '<style>';
        echo 'body{font-family:Arial;background:#f8fafc;margin:40px;}';
        echo '.card{background:white;padding:30px;border-radius:12px;';
        echo 'box-shadow:0 10px 25px rgba(0,0,0,0.08);}';
        echo 'h1{color:#dc2626;}';
        echo '</style>';
        echo '</head>';
        echo '<body>';
        echo '<div class="card">';
        echo '<h1>' . htmlspecialchars($title) . '</h1>';
        echo '<p>' . htmlspecialchars($message) . '</p>';
        echo '</div>';
        echo '</body>';
        echo '</html>';

        exit;

    }

}
