#Requires -Version 7.4

function Start-vSEALServiceAndDependencies {
    <#
    .SYNOPSIS
        Starts a Windows service and its dependencies.

    .DESCRIPTION
        Starts the specified service using the Invoke-vSEALServiceOperation helper.
        Handles dependent services and pending states automatically.

    .PARAMETER Name
        The name of the service to start.

    .PARAMETER InputObject
        A ServiceController object representing the service to start.

    .PARAMETER SkipDependentServices
        Skip starting dependent services.

    .PARAMETER PendingStatusWait
        Time to wait for pending states to resolve. Defaults to 60 seconds.

    .PARAMETER PassThru
        Return the service object after starting.

    .OUTPUTS
        [System.ServiceProcess.ServiceController] When -PassThru is specified.

    .EXAMPLE
        Start-vSEALServiceAndDependencies -Name 'Spooler'
        Starts the Print Spooler service and its dependencies.

    .EXAMPLE
        Start-vSEALServiceAndDependencies -Service 'wuauserv' -PassThru
        Starts Windows Update and returns the service object using the alias.

    .EXAMPLE
        Get-Service -Name 'Spooler' | Start-vSEALServiceAndDependencies
        Pipe a service object to start it.

    .EXAMPLE
        Start-vSEALServiceAndDependencies -Name 'MyService' -SkipDependentServices
        Starts only the specified service without starting dependents.

    .EXAMPLE
        Start-vSEALServiceAndDependencies -Name 'SlowService' -PendingStatusWait ([TimeSpan]::FromSeconds(120))
        Starts the service with a custom 2-minute timeout for pending states.
    #>
    [CmdletBinding(DefaultParameterSetName = 'ByName')]
    [OutputType([System.ServiceProcess.ServiceController])]
    param(
        [Parameter(Mandatory, Position = 0, ParameterSetName = 'ByName')]
        [ValidateNotNullOrEmpty()]
        [Alias('Service')]
        [string]$Name,

        [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'ByInputObject')]
        [ValidateNotNull()]
        [System.ServiceProcess.ServiceController]$InputObject,

        [Parameter()]
        [switch]$SkipDependentServices,

        [Parameter()]
        [TimeSpan]$PendingStatusWait = [TimeSpan]::FromSeconds(60),

        [Parameter()]
        [switch]$PassThru
    )

    process {
        # Determine the service name
        $serviceName = if ($PSCmdlet.ParameterSetName -eq 'ByInputObject') {
            $InputObject.Name
        }
        else {
            $Name
        }

        Write-vSEALLog -Message "Starting service [$serviceName] and dependencies..." -Level 'INFO'

        $operationParams = @{
            Name              = $serviceName
            Operation         = 'Start'
            PendingStatusWait = $PendingStatusWait
        }

        if ($SkipDependentServices) {
            $operationParams['SkipDependentServices'] = $true
        }

        if ($PassThru) {
            $operationParams['PassThru'] = $true
        }

        $result = Invoke-vSEALServiceOperation @operationParams

        if ($PassThru) {
            return $result
        }
    }
}
