Private/New-BridgeConfiguration.ps1

<#
.SYNOPSIS
Validates numeric bounds on configuration defaults loaded from overrides.

.DESCRIPTION
Ensures IntervalSeconds, MaxIterations, and MaxConsecutiveFailures remain integers within supported ranges after merging external configuration overrides.

.PARAMETER Defaults
The Defaults sub-object from the BridgeWatcher configuration.
#>

function Assert-BridgeConfigurationDefault {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNull()]
        [PSCustomObject]$Defaults
    )

    $defaultBounds = @{
        IntervalSeconds        = @(1, 3600)
        MaxIterations          = @(0, 1000)
        MaxConsecutiveFailures = @(1, 1440)
    }
    foreach ($boundName in $defaultBounds.Keys) {
        $bound = $defaultBounds[$boundName]
        $val = $Defaults.$boundName
        $isInteger = ($val -is [int]) -or ($val -is [long])
        if (-not $isInteger -or $val -lt $bound[0] -or $val -gt $bound[1]) {
            throw [System.ArgumentOutOfRangeException]::new(
                $boundName,
                $val,
                "Configuration Defaults.$boundName must be an integer between $($bound[0]) and $($bound[1])."
            )
        }
    }
}

<#
.SYNOPSIS
Imports external JSON configuration overrides into a configuration object.

.DESCRIPTION
Reads a JSON configuration file, merges any Urls and Defaults properties onto the target configuration, and validates numeric default bounds.

.PARAMETER Config
The BridgeWatcher configuration object to update.

.PARAMETER ConfigurationPath
The path to the JSON override file.
#>

function Import-BridgeConfigurationOverride {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNull()]
        [PSCustomObject]$Config,

        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ConfigurationPath
    )

    if (-not (Test-Path -LiteralPath $ConfigurationPath)) {
        throw [System.IO.FileNotFoundException]::new("Configuration file not found: $ConfigurationPath", $ConfigurationPath)
    }
    $rawJson = Get-Content -LiteralPath $ConfigurationPath -Raw -Encoding utf8
    $fileConfig = ConvertFrom-Json -InputObject $rawJson
    foreach ($section in @('Urls', 'Defaults')) {
        $sectionProp = $fileConfig.PSObject.Properties[$section]
        if ($null -eq $sectionProp -or $null -eq $sectionProp.Value) {
            continue
        }
        foreach ($prop in $sectionProp.Value.PSObject.Properties) {
            $Config.$section | Add-Member -MemberType NoteProperty -Name $prop.Name -Value $prop.Value -Force
        }
    }
    Assert-BridgeConfigurationDefault -Defaults $Config.Defaults
}

<#
.SYNOPSIS
Δημιουργεί ένα configuration object για το BridgeWatcher module.

.DESCRIPTION
Κεντρικοποιεί όλα τα configuration settings, URLs, patterns και mappings
που χρησιμοποιούνται στο BridgeWatcher module. Επιστρέφει ένα αντικείμενο
με Urls, Defaults, Statuses και υπο-αντικείμενα μηνυμάτων.

.PARAMETER BaseUrl
Το base URL για τα δεδομένα και εικόνες γεφυρών.

.PARAMETER OCRApiUrl
Το URL για τη Google Cloud Vision API.

.PARAMETER PushoverApiUrl
Το URL για την αποστολή Pushover notifications.

.PARAMETER DefaultIntervalSeconds
Το προεπιλεγμένο διάστημα σε δευτερόλεπτα για τη παρακολούθηση.

.PARAMETER DefaultMaxIterations
Ο προεπιλεγμένος μέγιστος αριθμός επαναλήψεων.

.PARAMETER MaxConsecutiveFailures
Ο μέγιστος αριθμός διαδοχικών αποτυχιών.

.PARAMETER LogDirectory
Ο κατάλογος αποθήκευσης αρχείων καταγραφής.

.PARAMETER ConfigurationPath
Προαιρετική διαδρομή σε εξωτερικό αρχείο JSON για παράκαμψη των προεπιλογών.

.OUTPUTS
[PSCustomObject] - Αντικείμενο διαμόρφωσης με Urls, Defaults, Statuses και υπο-αντικείμενα μηνυμάτων.

.NOTES
Αυτή η συνάρτηση δημιουργεί μόνο ένα configuration object στη μνήμη και δεν αλλάζει την κατάσταση του συστήματος.
#>

function New-BridgeConfiguration {
    [CmdletBinding()]
    [OutputType([PSCustomObject])]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Η συνάρτηση δημιουργεί μόνο το αντικείμενο διαμόρφωσης στη μνήμη, δεν αλλάζει την κατάσταση του συστήματος.')]

    param(
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$BaseUrl,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$OCRApiUrl,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]$PushoverApiUrl,

        [Parameter()]
        [ValidateRange(1, 3600)]
        [int]$DefaultIntervalSeconds = 300,

        [Parameter()]
        [ValidateRange(0, 1000)]
        [int]$DefaultMaxIterations = 100,

        [Parameter()]
        [ValidateRange(1, 1440)]
        [int]$MaxConsecutiveFailures = 60,

        [Parameter()]
        [string]$LogDirectory = '',

        [Parameter()]
        [string]$ConfigurationPath = ''
    )

    $testDriveLog = if (Test-Path -Path 'TestDrive:\') { 'TestDrive:\logs' } else { '' }
    $defaultLogDir = Join-Path -Path (Split-Path -Path (Split-Path -Path $PSScriptRoot -Parent) -Parent) -ChildPath 'logs'
    $LogDirectory = @($LogDirectory, $Env:BRIDGEWATCHER_OUT, $testDriveLog, $defaultLogDir) |
        Where-Object -FilterScript { -not [string]::IsNullOrWhiteSpace($_) } |
        ForEach-Object -Process { $_.Trim() } |
        Select-Object -First 1

    $sourceUrl = @($BaseUrl, 'https://www.topvision.gr/dioriga') | Where-Object -FilterScript { $_ } | Select-Object -First 1
    $ocrUrl = @($OCRApiUrl, 'https://vision.googleapis.com/v1/images:annotate') | Where-Object -FilterScript { $_ } | Select-Object -First 1
    $poUrl = @($PushoverApiUrl, 'https://api.pushover.net/1/messages.json') | Where-Object -FilterScript { $_ } | Select-Object -First 1

    $config = [PSCustomObject]@{
        Urls                      = [PSCustomObject]@{
            Source      = $sourceUrl.TrimEnd('/') + '/'
            BaseImage   = $sourceUrl.TrimEnd('/')
            OCRApi      = $ocrUrl
            PushoverApi = $poUrl
        }
        Defaults                  = [PSCustomObject]@{
            IntervalSeconds        = $DefaultIntervalSeconds
            MaxIterations          = $DefaultMaxIterations
            MaxConsecutiveFailures = $MaxConsecutiveFailures
            JsonDepth              = 5
            MaxWaitTimeMinutes     = 12
            HttpRequestTimeout     = 60
            LogDirectory           = $LogDirectory
            TimezoneIds            = @('GTB Standard Time', 'Europe/Athens')
        }
        Statuses                  = [PSCustomObject]@{
            Open                 = 'Ανοιχτή'
            Closed               = 'Κλειστή'
            ClosedForMaintenance = 'Κλειστή για συντήρηση'
            ClosedWithSchedule   = 'Κλειστή με πρόγραμμα'
            PermanentlyClosed    = 'Μόνιμα κλειστή'
            Unknown              = 'Άγνωστη'
        }
        BridgeNames               = @{
            'isthmia'    = 'Ισθμία'
            'poseidonia' = 'Ποσειδωνία'
        }
        ErrorMessages             = @{
            MonitoringError      = '❌ Σφάλμα κατά την ανάκτηση της κατάστασης της γέφυρας'
        }
        StatusMessages            = @{
            MonitoringStart    = 'Ξεκίνησε ο κύκλος παρακολούθησης'
            MonitoringComplete = '✅ Ο κύκλος παρακολούθησης ολοκληρώθηκε'
        }
        LoggingConfig             = @{
            ErrorStage   = 'Σφάλμα'
            InfoStage    = 'Ανάλυση'
            WarningLevel = 'Warning'
            DebugLevel   = 'Debug'
            VerboseLevel = 'Verbose'
        }
        ExportMessages            = @{
            Success            = '✅ Επιτυχής εξαγωγή JSON'
            Failed             = '❌ Αποτυχία εξαγωγής JSON'
            DirectoryNotExists = '❌ Ο φάκελος προορισμού δεν υπάρχει'
        }
        OCRMessages               = @{
            StartOCR  = '🔍 Εκκίνηση OCR'
            OCRFailed = '❌ Αποτυχία OCR'
        }
        PushoverMessages          = @{
            SendFailed = '❌ Αποτυχία αποστολής Pushover'
        }
        AdviceMessages            = @{
            DoNotWait = 'Είναι προτιμότερο να μην περιμένεις'
            Wait      = 'Είναι προτιμότερο να περιμένεις'
        }
    }

    if (-not [string]::IsNullOrWhiteSpace($ConfigurationPath)) {
        Import-BridgeConfigurationOverride -Config $config -ConfigurationPath $ConfigurationPath
    }

    $config.psobject.TypeNames.Insert(0, 'BridgeWatcher.Configuration')
    return $config
}