package certifiers

import (
	"fmt"

	"github.com/certimate-go/certimate/internal/domain"
	"github.com/certimate-go/certimate/pkg/core"
	chlgimpl "github.com/certimate-go/certimate/pkg/core/certifier/challengers/dns01/oraclecloud"
	xmaps "github.com/certimate-go/certimate/pkg/utils/maps"
)

func init() {
	ACMEDns01Registries.MustRegister(domain.ACMEDns01ProviderTypeOracleCloudDNS, func(options *ProviderFactoryOptions) (core.ACMEChallenger, error) {
		credentials := domain.AccessConfigForOracleCloud{}
		if err := xmaps.Populate(options.ProviderAccessConfig, &credentials); err != nil {
			return nil, fmt.Errorf("failed to populate provider access config: %w", err)
		}

		provider, err := chlgimpl.NewChallenger(&chlgimpl.ChallengerConfig{
			AuthMethod:            credentials.AuthMethod,
			PrivateKey:            credentials.PrivateKey,
			PrivateKeyPassphrase:  credentials.PrivateKeyPassphrase,
			PublicKeyFingerprint:  credentials.PublicKeyFingerprint,
			TenancyOcid:           credentials.TenancyOcid,
			UserOcid:              credentials.UserOcid,
			Region:                xmaps.GetString(options.ProviderExtendedConfig, "region"),
			CompartmentOcid:       xmaps.GetString(options.ProviderExtendedConfig, "compartmentOcid"),
			DnsPropagationTimeout: options.DnsPropagationTimeout,
			DnsTTL:                options.DnsTTL,
		})
		return provider, err
	})

	ACMEDns01Registries.MustRegisterAlias(domain.ACMEDns01ProviderTypeOracleCloud, domain.ACMEDns01ProviderTypeOracleCloudDNS)
}
