package domain

import (
	"net/url"
	"slices"
)

type BaseServer struct {
	key  string
	url  *url.URL
	open bool
}

const baseServerModelName = "BaseServer"

func NewBaseServer(key string, u *url.URL, open bool) (BaseServer, error) {
	if err := validateServerKey(key); err != nil {
		return BaseServer{}, ValidationError{
			Model: baseServerModelName,
			Field: "key",
			Err:   err,
		}
	}

	if u == nil {
		return BaseServer{}, ValidationError{
			Model: baseServerModelName,
			Field: "url",
			Err:   ErrNil,
		}
	}

	return BaseServer{
		key:  key,
		url:  u,
		open: open,
	}, nil
}

func (s BaseServer) Key() string {
	return s.key
}

func (s BaseServer) URL() *url.URL {
	return s.url
}

func (s BaseServer) Open() bool {
	return s.open
}

func (s BaseServer) IsZero() bool {
	return s == BaseServer{}
}

type BaseServers []BaseServer

func (ss BaseServers) FilterOutSpecial(specials Servers) BaseServers {
	res := make(BaseServers, 0, len(ss))

	for _, s := range ss {
		if slices.ContainsFunc(specials, func(special Server) bool {
			return special.Special() && special.Key() == s.Key()
		}) {
			continue
		}

		res = append(res, s)
	}

	return res
}
