package stack // https://github.com/docker/cli/blob/master/cli/command/stack/remove.go

import (
	"context"
	"fmt"
	"sort"
	"strings"

	"coopcloud.tech/abra/pkg/log"
	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/swarm"
	"github.com/docker/docker/api/types/versions"
	"github.com/docker/docker/client"
	apiclient "github.com/docker/docker/client"
	"github.com/pkg/errors"
)

// RunRemove is the swarm implementation of docker stack remove
func RunRemove(ctx context.Context, client *apiclient.Client, opts Remove) error {
	var errs []string
	for _, namespace := range opts.Namespaces {
		services, err := GetStackServices(ctx, client, namespace)
		if err != nil {
			return err
		}

		networks, err := getStackNetworks(ctx, client, namespace)
		if err != nil {
			return err
		}

		var secrets []swarm.Secret
		if versions.GreaterThanOrEqualTo(client.ClientVersion(), "1.25") {
			secrets, err = getStackSecrets(ctx, client, namespace)
			if err != nil {
				return err
			}
		}

		var configs []swarm.Config
		if versions.GreaterThanOrEqualTo(client.ClientVersion(), "1.30") {
			configs, err = getStackConfigs(ctx, client, namespace)
			if err != nil {
				return err
			}
		}

		if len(services)+len(networks)+len(secrets)+len(configs) == 0 {
			log.Warnf("nothing found in stack: %s", namespace)
			continue
		}

		hasError := removeServices(ctx, client, services)
		hasError = removeSecrets(ctx, client, secrets) || hasError
		hasError = removeConfigs(ctx, client, configs) || hasError
		hasError = removeNetworks(ctx, client, networks) || hasError

		if hasError {
			errs = append(errs, fmt.Sprintf("failed to remove some resources from stack: %s", namespace))
			continue
		}

		err = waitOnTasks(ctx, client, namespace)
		if err != nil {
			errs = append(errs, fmt.Sprintf("failed to wait on tasks of stack: %s: %s", namespace, err))
		}
	}

	if len(errs) > 0 {
		return errors.Errorf(strings.Join(errs, "\n"))
	}

	return nil
}

func sortServiceByName(services []swarm.Service) func(i, j int) bool {
	return func(i, j int) bool {
		return services[i].Spec.Name < services[j].Spec.Name
	}
}

func removeServices(
	ctx context.Context,
	client *apiclient.Client,
	services []swarm.Service,
) bool {
	var hasError bool
	sort.Slice(services, sortServiceByName(services))
	for _, service := range services {
		log.Infof("removing service %s", service.Spec.Name)
		if err := client.ServiceRemove(ctx, service.ID); err != nil {
			hasError = true
			log.Fatalf("failed to remove service %s: %s", service.ID, err)
		}
	}
	return hasError
}

func removeNetworks(
	ctx context.Context,
	client *apiclient.Client,
	networks []types.NetworkResource,
) bool {
	var hasError bool
	for _, network := range networks {
		log.Infof("removing network %s", network.Name)
		if err := client.NetworkRemove(ctx, network.ID); err != nil {
			hasError = true
			log.Fatalf("failed to remove network %s: %s", network.ID, err)
		}
	}
	return hasError
}

func removeSecrets(
	ctx context.Context,
	client *apiclient.Client,
	secrets []swarm.Secret,
) bool {
	var hasError bool
	for _, secret := range secrets {
		log.Infof("removing secret %s", secret.Spec.Name)
		if err := client.SecretRemove(ctx, secret.ID); err != nil {
			hasError = true
			log.Fatalf("Failed to remove secret %s: %s", secret.ID, err)
		}
	}
	return hasError
}

func removeConfigs(
	ctx context.Context,
	client *apiclient.Client,
	configs []swarm.Config,
) bool {
	var hasError bool
	for _, config := range configs {
		log.Infof("removing config %s", config.Spec.Name)
		if err := client.ConfigRemove(ctx, config.ID); err != nil {
			hasError = true
			log.Fatalf("failed to remove config %s: %s", config.ID, err)
		}
	}
	return hasError
}

// https://github.com/docker/cli/pull/4259
func getStackTasks(ctx context.Context, apiclient client.APIClient, namespace string) ([]swarm.Task, error) {
	return apiclient.TaskList(ctx, types.TaskListOptions{Filters: getStackFilter(namespace)})
}

var numberedStates = map[swarm.TaskState]int64{
	swarm.TaskStateNew:       1,
	swarm.TaskStateAllocated: 2,
	swarm.TaskStatePending:   3,
	swarm.TaskStateAssigned:  4,
	swarm.TaskStateAccepted:  5,
	swarm.TaskStatePreparing: 6,
	swarm.TaskStateReady:     7,
	swarm.TaskStateStarting:  8,
	swarm.TaskStateRunning:   9,
	swarm.TaskStateComplete:  10,
	swarm.TaskStateShutdown:  11,
	swarm.TaskStateFailed:    12,
	swarm.TaskStateRejected:  13,
}

func terminalState(state swarm.TaskState) bool {
	return numberedStates[state] > numberedStates[swarm.TaskStateRunning]
}

func waitOnTasks(ctx context.Context, client apiclient.APIClient, namespace string) error {
	terminalStatesReached := 0
	for {
		tasks, err := getStackTasks(ctx, client, namespace)
		if err != nil {
			return fmt.Errorf("failed to get tasks: %w", err)
		}

		for _, task := range tasks {
			if terminalState(task.Status.State) {
				terminalStatesReached++
				break
			}
		}

		if terminalStatesReached == len(tasks) {
			break
		}
	}
	return nil
}
