package app

import (
	"strings"

	"coopcloud.tech/abra/pkg/config"
	"github.com/sirupsen/logrus"
)

// Get retrieves an app
func Get(appName string) (config.App, error) {
	files, err := config.LoadAppFiles("")
	if err != nil {
		return config.App{}, err
	}

	app, err := config.GetApp(files, appName)
	if err != nil {
		return config.App{}, err
	}

	logrus.Debugf("retrieved %s for %s", app, appName)

	return app, nil
}

// deployedServiceSpec represents a deployed service of an app.
type deployedServiceSpec struct {
	Name    string
	Version string
}

// VersionSpec represents a deployed app and associated metadata.
type VersionSpec map[string]deployedServiceSpec

// ParseServiceName parses a $STACK_NAME_$SERVICE_NAME service label.
func ParseServiceName(label string) string {
	idx := strings.LastIndex(label, "_")
	serviceName := label[idx+1:]
	logrus.Debugf("parsed %s as service name from %s", serviceName, label)
	return serviceName
}