/*
Copyright © The ESO Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package certificatemanager

import (
	"context"
	"fmt"
	"strings"

	api "github.com/yandex-cloud/go-genproto/yandex/cloud/certificatemanager/v1"

	"github.com/external-secrets/external-secrets/providers/v1/yandex/certificatemanager/client"
	ydxcommon "github.com/external-secrets/external-secrets/providers/v1/yandex/common"
)

const (
	chainProperty              = "chain"
	privateKeyProperty         = "privateKey"
	chainAndPrivateKeyProperty = "chainAndPrivateKey"
)

// Implementation of ydxcommon.SecretGetter.
type certificateManagerSecretGetter struct {
	certificateManagerClient client.CertificateManagerClient
}

func newCertificateManagerSecretGetter(certificateManagerClient client.CertificateManagerClient) (ydxcommon.SecretGetter, error) {
	return &certificateManagerSecretGetter{
		certificateManagerClient: certificateManagerClient,
	}, nil
}

func (g *certificateManagerSecretGetter) GetSecret(ctx context.Context, iamToken, resourceID string, resourceKeyType ydxcommon.ResourceKeyType, folderID, versionID, property string) ([]byte, error) {
	response, err := g.fetchCertificateContentResponse(ctx, iamToken, resourceID, resourceKeyType, folderID, versionID)
	if err != nil {
		return nil, fmt.Errorf("unable to request certificate content to get secret: %w", err)
	}
	chain := trimAndJoin(response.CertificateChain...)
	privateKey := trimAndJoin(response.PrivateKey)

	switch property {
	case "", chainAndPrivateKeyProperty:
		return []byte(trimAndJoin(chain, privateKey)), nil
	case chainProperty:
		return []byte(chain), nil
	case privateKeyProperty:
		return []byte(privateKey), nil
	default:
		return nil, fmt.Errorf("unsupported property '%s'", property)
	}
}

func (g *certificateManagerSecretGetter) GetSecretMap(
	ctx context.Context,
	iamToken, resourceID string,
	resourceKeyType ydxcommon.ResourceKeyType,
	folderID, versionID string,
) (map[string][]byte, error) {
	response, err := g.fetchCertificateContentResponse(ctx, iamToken, resourceID, resourceKeyType, folderID, versionID)
	if err != nil {
		return nil, fmt.Errorf("unable to request certificate content to get secret map: %w", err)
	}
	chain := strings.Join(response.CertificateChain, "\n")
	privateKey := response.PrivateKey

	return map[string][]byte{
		chainProperty:      []byte(chain),
		privateKeyProperty: []byte(privateKey),
	}, nil
}

func (g *certificateManagerSecretGetter) fetchCertificateContentResponse(
	ctx context.Context,
	iamToken, resourceID string,
	resourceKeyType ydxcommon.ResourceKeyType,
	folderID, versionID string,
) (*api.GetCertificateContentResponse, error) {
	switch resourceKeyType {
	case ydxcommon.ResourceKeyTypeID:
		return g.certificateManagerClient.GetCertificateContent(ctx, iamToken, resourceID, versionID)
	case ydxcommon.ResourceKeyTypeName:
		responseEx, err := g.certificateManagerClient.GetExCertificateContent(ctx, iamToken, folderID, resourceID, versionID)
		if err != nil {
			return nil, err
		}
		return convertToGetCertificateContentResponse(responseEx), nil
	default:
		return nil, fmt.Errorf("unsupported resource key type '%v'", resourceKeyType)
	}
}

func trimAndJoin(elems ...string) string {
	var sb strings.Builder
	for _, elem := range elems {
		sb.WriteString(strings.TrimSpace(elem))
		sb.WriteRune('\n')
	}
	return sb.String()
}

func convertToGetCertificateContentResponse(response *api.GetExCertificateContentResponse) *api.GetCertificateContentResponse {
	return &api.GetCertificateContentResponse{
		CertificateId:    response.CertificateId,
		CertificateChain: response.CertificateChain,
		PrivateKey:       response.PrivateKey,
	}
}
