<?php

header('Content-Type: application/json; charset=utf-8');

$apacheVersion = function_exists('apache_get_version')? apache_get_version(): ($_SERVER['SERVER_SOFTWARE'] ?? 'No disponible');

$serverIp = $_SERVER['SERVER_ADDR'] ?? gethostbyname(gethostname());
$readmePath = __DIR__ . '/readme.txt';
$versionPermisos = 'No disponible';

if (is_readable($readmePath)) {
    $readmeContent = trim(file_get_contents($readmePath));

    if (preg_match('/\d+(?:\.\d+)+/', $readmeContent, $matches)) {
        $versionPermisos = $matches[0];
    } elseif ($readmeContent !== '') {
        $versionPermisos = $readmeContent;
    }
}

$response = array(
    'estatus' => 'OK',
    'mensaje' => 'El servicio responde correctamente.',
    'version_servicios' => $versionPermisos,
    'servidor' => array(
        'host' => gethostname(),
        'ip' => $serverIp,
        'puerto' => $_SERVER['SERVER_PORT'] ?? 'No disponible',
        'software' => $_SERVER['SERVER_SOFTWARE'] ?? 'No disponible',
        'apache' => $apacheVersion,
        'php' => PHP_VERSION
    ),
    'fecha_hora' => date('Y-m-d H:i:s')
);

echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
