package admin

import (
	"strings"
)

func (h *Handler) providerNameTaken(name string, excludeID uint) bool {
	name = strings.TrimSpace(name)
	if name == "" {
		return false
	}
	for _, p := range h.cache.ListProviders() {
		if p.ID != excludeID && p.Name == name {
			return true
		}
	}
	return false
}

func (h *Handler) modelAliasTaken(alias string, excludeModelID uint) bool {
	alias = strings.TrimSpace(alias)
	if alias == "" {
		return false
	}
	for _, p := range h.cache.ListProviders() {
		for _, m := range h.cache.GetModels(p.ID) {
			if m.ID == excludeModelID {
				continue
			}
			if m.Alias == alias {
				return true
			}
		}
	}
	return false
}

func normalizeAlias(alias string) string {
	return strings.TrimSpace(alias)
}

func validateModelAlias(h *Handler, alias string, excludeModelID uint) string {
	alias = normalizeAlias(alias)
	if alias == "" {
		return ""
	}
	if h.modelAliasTaken(alias, excludeModelID) {
		return "model alias already exists"
	}
	return ""
}
