package pki

import (
	"log"
	"net/url"

	"git.paulbsd.com/paulbsd/pki/src/config"
	"github.com/go-acme/lego/v4/providers/dns/ovh"
	"github.com/go-acme/lego/v4/providers/dns/pdns"
)

// initOVHProvider initialize DNS provider configuration
func initOVHProvider(cfg *config.Config) (ovhprovider *ovh.DNSProvider, err error) {
	ovhconfig := ovh.NewDefaultConfig()

	ovhconfig.APIEndpoint = cfg.ACME.ProviderOptions["ovhendpoint"]
	ovhconfig.ApplicationKey = cfg.ACME.ProviderOptions["ovhak"]
	ovhconfig.ApplicationSecret = cfg.ACME.ProviderOptions["ovhas"]
	ovhconfig.ConsumerKey = cfg.ACME.ProviderOptions["ovhck"]

	ovhprovider, err = ovh.NewDNSProviderConfig(ovhconfig)
	if err != nil {
		log.Println(err)
	}

	return
}

// initPowerDNSProvider initialize DNS provider configuration
func initPowerDNSProvider(cfg *config.Config) (pdnsprovider *pdns.DNSProvider, err error) {
	pdnsconfig := pdns.NewDefaultConfig()

	pdnsconfig.Host, err = url.Parse(cfg.ACME.ProviderOptions["pdnsapiurl"])
	pdnsconfig.APIKey = cfg.ACME.ProviderOptions["pdnsapikey"]

	pdnsprovider, err = pdns.NewDNSProviderConfig(pdnsconfig)
	if err != nil {
		log.Println(err)
	}

	return
}
