package domainproviderimp

import (
	"net/url"
	"os"

	"github.com/tech/sendico/pkg/mlogger"
	"go.uber.org/zap"
)

type DomainProviderImp struct {
	domain      string
	apiEndpoint string
}

func (dp *DomainProviderImp) GetFullLink(linkElem ...string) (string, error) {
	return url.JoinPath(dp.domain, linkElem...)
}

func (dp *DomainProviderImp) GetAPILink(linkElem ...string) (string, error) {
	return url.JoinPath(dp.apiEndpoint, linkElem...)
}

func CreateDomainProviderImp(logger mlogger.Logger, domain, protocol, apiPointEnv string) (*DomainProviderImp, error) {
	p := new(DomainProviderImp)
	p.domain = os.Getenv(protocol) + "://" + os.Getenv(domain)
	var err error
	apiPath := os.Getenv(apiPointEnv)
	if p.apiEndpoint, err = url.JoinPath(p.domain, apiPath); err != nil {
		logger.Error("Failed to create domain provider", zap.String("domain", p.domain), zap.String("api_path", apiPath))
		return nil, err
	}
	logger.Named("domain_provider").Info("Domain name provider is up", zap.String("domain", p.domain))
	return p, nil
}
