package deployers

import (
	"fmt"

	"github.com/certimate-go/certimate/internal/domain"
	"github.com/certimate-go/certimate/pkg/core"
	dplyimpl "github.com/certimate-go/certimate/pkg/core/deployer/providers/oraclecloud-certificatesmgmt"
	xmaps "github.com/certimate-go/certimate/pkg/utils/maps"
)

func init() {
	Registries.MustRegister(domain.DeploymentProviderTypeOracleCloudCertificatesMgmt, func(options *ProviderFactoryOptions) (core.Deployer, 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 := dplyimpl.NewDeployer(&dplyimpl.DeployerConfig{
			AuthMethod:           credentials.AuthMethod,
			PrivateKey:           credentials.PrivateKey,
			PrivateKeyPassphrase: credentials.PrivateKeyPassphrase,
			PublicKeyFingerprint: credentials.PublicKeyFingerprint,
			TenancyOcid:          credentials.TenancyOcid,
			UserOcid:             credentials.UserOcid,
			CompartmentOcid:      xmaps.GetString(options.ProviderExtendedConfig, "compartmentOcid"),
		})
		return provider, err
	})
}
