#Requires -Version 7.4

function Stop-vSEALServiceAndDependencies {
    <#
    .SYNOPSIS
        Stops a Windows service and its dependents.

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

    .PARAMETER Name
        The name of the service to stop.

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

    .PARAMETER SkipDependentServices
        Skip stopping dependent services.

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

    .PARAMETER PassThru
        Return the service object after stopping.

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

    .EXAMPLE
        Stop-vSEALServiceAndDependencies -Name 'Spooler'
        Stops the Print Spooler service and its dependents.

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

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

    .EXAMPLE
        Stop-vSEALServiceAndDependencies -Name 'MyService' -SkipDependentServices
        Stops only the specified service without stopping dependents.

    .EXAMPLE
        Stop-vSEALServiceAndDependencies -Name 'SlowService' -PendingStatusWait ([TimeSpan]::FromSeconds(120))
        Stops 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 "Stopping service [$serviceName] and dependents..." -Level 'INFO'

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

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

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

        $result = Invoke-vSEALServiceOperation @operationParams

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