#Requires -Version 7.4

function Get-vSEALServiceStartMode {
    <#
    .SYNOPSIS
        Gets the startup mode of a Windows service.

    .DESCRIPTION
        Retrieves the startup mode for a specified service, including detection
        of the "Automatic (Delayed Start)" configuration using CIM/WMI.

    .PARAMETER Service
        The ServiceController object or name of the service to query.
        Throws an error if the service does not exist.

    .OUTPUTS
        [string] The start mode: 'Automatic', 'Automatic (Delayed Start)', 'Manual',
        'Disabled', 'Boot', or 'System'.

    .EXAMPLE
        Get-vSEALServiceStartMode -Service 'Spooler'
        Returns 'Automatic' if the Print Spooler service is set to start automatically.

    .EXAMPLE
        Get-vSEALServiceStartMode -Service 'wuauserv'
        Returns 'Automatic (Delayed Start)' if Windows Update is configured for delayed start.

    .EXAMPLE
        $service = Get-Service -Name 'Spooler'
        Get-vSEALServiceStartMode -Service $service
        Pass a ServiceController object directly.

    .EXAMPLE
        $mode = Get-vSEALServiceStartMode -Service 'SomeService'
        if ($mode -eq 'Disabled') {
            Write-Host 'Service is disabled'
        }
        Check and handle service start mode.
    #>
    [CmdletBinding()]
    [OutputType([string])]
    param(
        [Parameter(Mandatory, Position = 0)]
        [ValidateNotNull()]
        [ValidateScript({
            if ($_ -is [System.ServiceProcess.ServiceController]) {
                return $true
            }
            if ($_ -is [string]) {
                $svc = Get-Service -Name $_ -ErrorAction SilentlyContinue
                if (-not $svc) {
                    throw "Service '$_' does not exist on this computer."
                }
                return $true
            }
            throw "Parameter must be a ServiceController object or service name string."
        })]
        [Alias('Name')]
        $Service
    )

    # Resolve service object and name from either input type
    if ($Service -is [System.ServiceProcess.ServiceController]) {
        $serviceObj = $Service
        $serviceName = $Service.Name
    }
    else {
        $serviceName = $Service
        $serviceObj = Get-Service -Name $serviceName -ErrorAction Stop
    }

    Write-vSEALLog -Message "Getting start mode for service [$serviceName]..." -Level 'VERBOSE'

    # Get the base start type
    $startType = $serviceObj.StartType.ToString()

    # Check for delayed start only if the service is Automatic
    if ($startType -eq 'Automatic') {
        try {
            $cimService = Get-CimInstance -ClassName Win32_Service -Filter "Name='$serviceName'" -ErrorAction Stop
            if ($cimService.DelayedAutoStart -eq $true) {
                Write-vSEALLog -Message "Service [$serviceName] start mode: Automatic (Delayed Start)" -Level 'VERBOSE'
                return 'Automatic (Delayed Start)'
            }
        }
        catch {
            Write-vSEALLog -Message "Failed to query CIM for delayed start status of [$serviceName]: $($_.Exception.Message)" -Level 'VERBOSE'
            # Fall through to return basic Automatic
        }
    }

    Write-vSEALLog -Message "Service [$serviceName] start mode: $startType" -Level 'VERBOSE'
    return $startType
}
