# PowerShell 7 native logging module
# Replaces log4net (incompatible with .NET Core / PS7) with simple file-based logging
# Maintains the same exported function signatures for full backward compatibility

$script:logFilePath = $null

function InitializeLogging
{
	Param
	(
		[string]$log4netdll,
		[string]$log4netconfig,
		[string]$logPath = $(throw "logPath required.")
	)

	# Create log directory if it doesn't exist
	if (!(Test-Path $logPath))
	{
		New-Item -ItemType Directory -Path $logPath -Force | Out-Null
	}

	# Use same date-based log file pattern as the original log4net config
	$dateStamp = Get-Date -Format "yyyy-MM-dd"
	$script:logFilePath = Join-Path $logPath "$dateStamp.log"
}

function Write-LogEntry($level, $message)
{
	if ($null -ne $script:logFilePath)
	{
		try
		{
			$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss,fff"
			$entry = "$timestamp $($level.PadRight(5)) $message"
			$entry | Out-File -FilePath $script:logFilePath -Append -Encoding utf8
		}
		catch
		{
			# Silently ignore log write failures - don't break the actual script
		}
	}
}

function Info($message)
{
	Write-Host "Info : $message" -ForegroundColor Green
	Write-LogEntry "INFO" $message
}

function Debug($message)
{
	Write-Host "Debug : $message"
	Write-LogEntry "DEBUG" $message
}

function Warn($message)
{
	Write-Host "Warning : $message" -ForegroundColor Yellow
	Write-LogEntry "WARN" $message
}

function Error($message, $exception, [System.Management.Automation.InvocationInfo]$invocationInfo)
{
	if (![string]::IsNullOrEmpty($exception))
	{
		Write-Host "Error" -ForegroundColor Red
		Write-Host "  Message          : $message" -ForegroundColor Red
		$message = WriteErrorDetails $message $exception $invocationInfo
	}
	else
	{
		Write-Host "Error : $message" -ForegroundColor Red
	}

	Write-LogEntry "ERROR" $message
}

function WriteErrorDetails($message, $exception, [System.Management.Automation.InvocationInfo]$invocationInfo)
{
	$positionMessage = $null
	if ($null -ne $invocationInfo)
	{
		Write-Host "  Script           : $($invocationInfo.ScriptName)" -ForegroundColor Red
		Write-Host "  Linenumber       : $($invocationInfo.ScriptLineNumber)" -ForegroundColor Red
		Write-Host "  Line             : $($invocationInfo.Line.Trim())" -ForegroundColor Red
		$positionMessage = $invocationInfo.PositionMessage
	}

	Write-Host "  Stacktrace       : $($exception.StackTrace)" -ForegroundColor Red
	if ($null -ne $exception.InnerException)
	{
		Write-Host "    InnerException : $($exception.InnerException)" -ForegroundColor Red
		Write-Host "    Stacktrace     : $($exception.InnerException.StackTrace)" -ForegroundColor Red
	}

	if ($null -ne $positionMessage)
	{
		$message = $message + "; Details: $positionMessage"
	}

	return $message
}

function Fatal($message, $exception, [System.Management.Automation.InvocationInfo]$invocationInfo)
{
	Write-Host "Fatal" -ForegroundColor Red
	Write-Host "  Message          : $message" -ForegroundColor Red
	$message = WriteErrorDetails $message $exception $invocationInfo
	Write-LogEntry "FATAL" $message
}

function InfoEvent($message)
{
	Write-Host "InfoEvent : $message" -ForegroundColor Green
	Write-LogEntry "INFO" "[Event] $message"
}

function DebugEvent($message)
{
	Write-Host "DebugEvent : $message"
	Write-LogEntry "DEBUG" "[Event] $message"
}

function WarnEvent($message)
{
	Write-Host "WarnEvent : $message" -ForegroundColor Yellow
	Write-LogEntry "WARN" "[Event] $message"
}

function ErrorEvent($message, $exception, [System.Management.Automation.InvocationInfo]$invocationInfo)
{
	Write-Host "ErrorEvent" -ForegroundColor Red
	Write-Host "  Message          : $message" -ForegroundColor Red
	$message = WriteErrorDetails $message $exception $invocationInfo
	Write-LogEntry "ERROR" "[Event] $message"
}

function FatalEvent($message, $exception, [System.Management.Automation.InvocationInfo]$invocationInfo)
{
	Write-Host "FatalEvent" -ForegroundColor Red
	Write-Host "  Message          : $message" -ForegroundColor Red
	$message = WriteErrorDetails $message $exception $invocationInfo
	Write-LogEntry "FATAL" "[Event] $message"
}

function Get-ScriptDirectory
{
	Split-Path $script:MyInvocation.MyCommand.Path
}

function GetLogfileName
{
	return $script:logFilePath
}

Export-ModuleMember -Function InitializeLogging, Info, Debug, Warn, Error, Fatal, InfoEvent, DebugEvent, WarnEvent, ErrorEvent, FatalEvent
Export-ModuleMember -Alias "*"
