<?php

function custom_log(string $message, string $level = 'INFO'): void
{
    $logDir = __DIR__ . '/../logs';
    
    if (!is_dir($logDir)) {
        mkdir($logDir, 0755, true);
    }
    
    $logFile = $logDir . '/app.log';
    $timestamp = date('Y-m-d H:i:s');
    $ip = $_SERVER['REMOTE_ADDR'] ?? 'CLI';
    $user = $_SESSION['username'] ?? 'guest';
    
    $logMessage = "[{$timestamp}] [{$level}] [{$ip}] [{$user}] {$message}" . PHP_EOL;
    
    file_put_contents($logFile, $logMessage, FILE_APPEND);
}

function logError(string $message): void
{
    custom_log($message, 'ERROR');
}

function logWarning(string $message): void
{
    custom_log($message, 'WARNING');
}

function logInfo(string $message): void
{
    custom_log($message, 'INFO');
}

function logDebug(string $message): void
{
    if ($_ENV['APP_ENVIRONMENT'] ?? getenv('APP_ENVIRONMENT') === 'pruebas') {
        custom_log($message, 'DEBUG');
    }
}
