package cmd

import (
	"fmt"
	"sync"

	"github.com/SurgeDM/Surge/internal/utils"
)

var (
	globalShutdownOnce sync.Once
	globalShutdownErr  error
	globalShutdownFn   = defaultGlobalShutdown
)

func defaultGlobalShutdown() error {
	cancelGlobalEnqueue()

	// Shutdown the service FIRST so that PauseAll() can emit DownloadPausedMsg
	// events while the lifecycle event worker is still alive to persist them.
	// If we close the lifecycle stream before shutdown, pause state is lost
	// and downloads vanish from the list on terminal close.
	var err error
	if GlobalService != nil {
		err = GlobalService.Shutdown()
	} else if GlobalPool != nil {
		GlobalPool.GracefulShutdown()
	}

	if cleanup := takeLifecycleCleanup(); cleanup != nil {
		cleanup()
	}

	return err
}

func executeGlobalShutdown(reason string) error {
	globalShutdownOnce.Do(func() {
		utils.Debug("Executing graceful shutdown (%s)", reason)
		globalShutdownErr = globalShutdownFn()
		if globalShutdownErr != nil {
			globalShutdownErr = fmt.Errorf("graceful shutdown failed: %w", globalShutdownErr)
		}
	})
	return globalShutdownErr
}

func resetGlobalShutdownCoordinatorForTest(fn func() error) {
	globalShutdownOnce = sync.Once{}
	globalShutdownErr = nil
	resetGlobalEnqueueContext()
	_ = takeLifecycleCleanup()
	if fn != nil {
		globalShutdownFn = fn
		return
	}
	globalShutdownFn = defaultGlobalShutdown
}
