<?php

/**
 * Loads environment variables from a .env file.
 *
 * @param string $path Path to the .env file.
 * @return void
 * @throws Exception If the .env file doesn't exist.
 */
function loadEnv(string $path): void
{
    if (!file_exists($path)) {
        throw new Exception("The .env file does not exist: $path");
    }

    $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    foreach ($lines as $line) {
        // Ignore comments
        if (str_contains($line, '#')) {
            $line = substr($line, 0, strpos($line, '#'));
        }

        // Process key=value lines
        if (str_contains($line, '=')) {
            list($key, $value) = explode('=', $line, 2);
            $key = trim($key);
            $value = trim($value);

            // Remove surrounding quotes
            if (str_starts_with($value, "'") && str_ends_with($value, "'")) {
                $value = substr($value, 1, -1);
            } elseif (str_starts_with($value, '"') && str_ends_with($value, '"')) {
                $value = substr($value, 1, -1);
            }

            // Set environment variable
            putenv(sprintf('%s=%s', $key, $value));
        }
    }
}

/**
 * Parses an environment value and converts it to an appropriate data type.
 *
 * @param mixed $value Input value.
 * @return mixed The converted value.
 */
function parse_env_value(mixed $value): mixed
{
    // Convert boolean strings
    if (strtolower($value) === 'true') {
        return true;
    }
    if (strtolower($value) === 'false') {
        return false;
    }

    // Convert numeric strings
    if (is_numeric($value)) {
        return $value + 0;
    }

    return $value;
}

try {
    loadEnv(__DIR__ . '/../../.env');
} catch (Exception $e) {
    exit($e->getMessage());
}

date_default_timezone_set("America/Mexico_City");

ini_set('max_execution_time', 300000);

// Application settings
define("APP_NAME", getenv('APP_NAME'));
define("APP_SHORT_NAME", getenv('APP_SHORT_NAME'));
define("APP_DEBUG", parse_env_value(getenv('APP_DEBUG')));
define("APP_URL", getenv('APP_URL'));

// Production Elasticsearch settings
define("ES_SERVER", getenv('ES_SERVER'));
define("ES_USERNAME", getenv('ES_USERNAME'));
define("ES_PASSWORD", getenv('ES_PASSWORD'));
define("ES_INDEX", getenv('ES_INDEX'));
define("ES_INDEX_LOG", getenv('ES_INDEX_LOG'));

// Development Elasticsearch settings
define("DEV_ES_SERVER", getenv('DEV_ES_SERVER'));
define("DEV_ES_USERNAME", getenv('DEV_ES_USERNAME'));
define("DEV_ES_PASSWORD", getenv('DEV_ES_PASSWORD'));
define("DEV_ES_INDEX", getenv('DEV_ES_INDEX'));
define("DEV_ES_INDEX_LOG", getenv('DEV_ES_INDEX_LOG'));

// Email settings
define("MAIL_HOST", getenv('MAIL_HOST'));
define("MAIL_DEBUG", getenv('MAIL_DEBUG'));
define("MAIL_AUTH", parse_env_value(getenv('MAIL_AUTH')));
define("MAIL_PORT", getenv('MAIL_PORT'));
define("MAIL_USERNAME", getenv('MAIL_USERNAME'));
define("MAIL_PASSWORD", getenv('MAIL_PASSWORD'));
define("MAIL_ENCRYPTION", getenv('MAIL_ENCRYPTION'));
define("MAIL_FROM", getenv('MAIL_FROM'));
define("MAIL_FROM_NAME", getenv('MAIL_FROM_NAME'));
define("MAIL_BLOCK_DEV", parse_env_value(getenv('MAIL_BLOCK_DEV')));

// Security settings
define("CIPHER_METHOD", getenv('CIPHER_METHOD'));

// Branding
define("LOGO_URL", getenv('LOGO_URL'));
