<?php

$env = [];

// Load .env from project root (server/)
$env_path = dirname(__DIR__) . '/.env';
if (file_exists($env_path)) {
	$lines = file($env_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	foreach ($lines as $line) {
		$line = trim($line);
		if ($line === '' || $line[0] === '#') continue;
		if (strpos($line, '=') === false) continue;
		list($key, $value) = explode('=', $line, 2);
		$env[trim($key)] = trim($value);
	}
}

return [
	"app_name" => $env['APP_NAME'] ?? "SHServ",
	"version" => $env['APP_VERSION'] ?? "0.3 dev",
	"debug" => ($env['DEBUG'] ?? "false") === "true",
	"default_db_wrap" => false,
	"db" => [
		"dblib" => $env['DB_DRIVER'] ?? "mysql",
		"host" => $env['DB_HOST'] ?? "localhost",
		"dbname" => $env['DB_NAME'] ?? "smart-home-server",
		"charset" => $env['DB_CHARSET'] ?? "utf8",
		"user" => $env['DB_USER'] ?? "root",
		"password" => $env['DB_PASSWORD'] ?? ""
	],
	"app_file" => "App.php",
	"templates_folder" => "Templates",
	"logs_enable" => true,
	"logs_folder" => dirname(__DIR__, 2) . "/Logs",
	"logs_min_level" => $env['LOG_LEVEL'] ?? "INFO",
	"logs_sources" => [],
	"logs_max_file_size_mb" => 50,
	"logs_max_days" => 30,
	"devmode" => ($env['DEVMODE'] ?? "false") === "true",

	"controllers_folder" => "Controllers",
	"text_msgs" => require_once("SHServ/text-msgs.php"),

	"error_handler" => [
		"important_errors" => ["E_WARNING", "E_ERROR", "E_CORE_ERROR", "EXCEPTION"]
	],

	"device_ip_range" => [
		$env['DEVICE_IP_RANGE_START'] ?? "192.168.2.2",
		$env['DEVICE_IP_RANGE_END'] ?? "192.168.2.254"
	],
	"device_api_connect_timeout" => (float)($env['DEVICE_API_CONNECT_TIMEOUT'] ?? "1"),
	"device_api_timeout" => (float)($env['DEVICE_API_TIMEOUT'] ?? "5"),
	"device_offline_threshold" => (int)($env['DEVICE_OFFLINE_THRESHOLD'] ?? "300"),
	"firmwares_dir" => $env['FIRMWARES_DIR'] ?? dirname(__DIR__, 2) . "/firmwares",

	"gauth" => [
		"base_url" => $env['GAUTH_BASE_URL'] ?? "https://auth.gnexus.space",
		"client_id" => $env['GAUTH_CLIENT_ID'] ?? "shserv",
		"client_secret" => $env['GAUTH_CLIENT_SECRET'] ?? "",
		"redirect_uri" => $env['GAUTH_REDIRECT_URI'] ?? "https://smarthome.gnexus.space/auth/callback",
		"webhook_secret" => $env['GAUTH_WEBHOOK_SECRET'] ?? "",
	],
];
