package container

import (
	"context"
	"fmt"
	"strings"

	"coopcloud.tech/abra/pkg/formatter"
	"coopcloud.tech/abra/pkg/log"
	"github.com/AlecAivazis/survey/v2"
	"github.com/docker/docker/api/types"
	containerTypes "github.com/docker/docker/api/types/container"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/client"
)

// GetContainer retrieves a container. If noInput is false and the retrievd
// count of containers does not match 1, then a prompt is presented to let the
// user choose. A count of 0 is handled gracefully.
func GetContainer(c context.Context, cl *client.Client, filters filters.Args, noInput bool) (types.Container, error) {
	containerOpts := containerTypes.ListOptions{Filters: filters}
	containers, err := cl.ContainerList(c, containerOpts)
	if err != nil {
		return types.Container{}, err
	}

	if len(containers) == 0 {
		filter := filters.Get("name")[0]
		return types.Container{}, fmt.Errorf("no containers matching the %v filter found?", filter)
	}

	if len(containers) > 1 {
		var containersRaw []string
		for _, container := range containers {
			containerName := strings.Join(container.Names, " ")
			trimmed := strings.TrimPrefix(containerName, "/")
			created := formatter.HumanDuration(container.Created)
			containersRaw = append(containersRaw, fmt.Sprintf("%s (created %v)", trimmed, created))
		}

		if noInput {
			err := fmt.Errorf("expected 1 container but found %v: %s", len(containers), strings.Join(containersRaw, " "))
			return types.Container{}, err
		}

		log.Warnf("ambiguous container list received, prompting for input")

		var response string
		prompt := &survey.Select{
			Message: "which container are you looking for?",
			Options: containersRaw,
		}

		if err := survey.AskOne(prompt, &response); err != nil {
			return types.Container{}, err
		}

		chosenContainer := strings.TrimSpace(strings.Split(response, " ")[0])
		for _, container := range containers {
			containerName := strings.TrimSpace(strings.Join(container.Names, " "))
			trimmed := strings.TrimPrefix(containerName, "/")
			if trimmed == chosenContainer {
				return container, nil
			}
		}

		log.Fatal("failed to match chosen container")
	}

	return containers[0], nil
}

// GetContainerFromStackAndService retrieves the container for the given stack and service.
func GetContainerFromStackAndService(cl *client.Client, stack, service string) (types.Container, error) {
	filters := filters.NewArgs()
	filters.Add("name", fmt.Sprintf("^%s_%s", stack, service))

	container, err := GetContainer(context.Background(), cl, filters, true)
	if err != nil {
		return types.Container{}, err
	}
	return container, nil
}
