package huaweicloudapig

import (
	"context"
	"fmt"
	"log/slog"
	"time"

	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
	hwapigmodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/apig/v2/model"
	hwapigregion "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/apig/v2/region"
	hwiam "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iam/v3"
	hwiammodel "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iam/v3/model"
	hwiamregion "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iam/v3/region"
	"github.com/samber/lo"

	hwapig "github.com/certimate-go/certimate/pkg/sdk3rd-trimmed/github.com/huaweicloud/huaweicloud-sdk-go-v3/services/apig/v2"

	"github.com/certimate-go/certimate/pkg/core"
)

type (
	Provider     = core.Deployer
	DeployResult = core.DeployerDeployResult
)

type DeployerConfig struct {
	// 华为云 AccessKeyId。
	AccessKeyId string `json:"accessKeyId"`
	// 华为云 SecretAccessKey。
	SecretAccessKey string `json:"secretAccessKey"`
	// 华为云企业项目 ID。
	EnterpriseProjectId string `json:"enterpriseProjectId,omitempty"`
	// 华为云区域。
	Region string `json:"region"`
	// 部署目标。
	DeployTarget string `json:"deployTarget"`
	// 证书 ID。
	// 部署目标为 [DEPLOY_TARGET_CERTIFICATE] 时必填。
	CertificateId string `json:"certificateId,omitempty"`
}

type Deployer struct {
	config    *DeployerConfig
	logger    *slog.Logger
	sdkClient *hwapig.ApigClient
}

var _ Provider = (*Deployer)(nil)

func NewDeployer(config *DeployerConfig) (*Deployer, error) {
	if config == nil {
		return nil, fmt.Errorf("the configuration of the deployer provider is nil")
	}

	client, err := createSDKClient(
		config.AccessKeyId,
		config.SecretAccessKey,
		config.Region,
	)
	if err != nil {
		return nil, fmt.Errorf("could not create client: %w", err)
	}

	return &Deployer{
		config:    config,
		logger:    slog.Default(),
		sdkClient: client,
	}, nil
}

func (d *Deployer) SetLogger(logger *slog.Logger) {
	if logger == nil {
		d.logger = slog.New(slog.DiscardHandler)
	} else {
		d.logger = logger
	}
}

func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*DeployResult, error) {
	// 根据部署目标决定业务流程
	switch d.config.DeployTarget {
	case DEPLOY_TARGET_CERTIFICATE:
		if err := d.deployToCertificate(ctx, certPEM, privkeyPEM); err != nil {
			return nil, err
		}

	default:
		return nil, fmt.Errorf("unsupported deploy target '%s'", d.config.DeployTarget)
	}

	return &DeployResult{}, nil
}

func (d *Deployer) deployToCertificate(ctx context.Context, certPEM, privkeyPEM string) error {
	if d.config.CertificateId == "" {
		return fmt.Errorf("config `certificateId` is required")
	}

	// 查询证书详情
	// REF: https://support.huaweicloud.com/api-apig/ShowDetailsOfCertificateV2.html
	showDetailsOfCertificateV2Req := &hwapigmodel.ShowDetailsOfCertificateV2Request{
		CertificateId: d.config.CertificateId,
	}
	showDetailsOfCertificateV2Resp, err := d.sdkClient.ShowDetailsOfCertificateV2(showDetailsOfCertificateV2Req)
	d.logger.Debug("sdk request 'apig.ShowDetailsOfCertificateV2'", slog.Any("request", showDetailsOfCertificateV2Req), slog.Any("response", showDetailsOfCertificateV2Resp))
	if err != nil {
		return fmt.Errorf("failed to execute sdk request 'apig.ShowDetailsOfCertificateV2': %w", err)
	}

	// 修改 SSL 证书
	// REF: https://support.huaweicloud.com/api-apig/UpdateCertificateV2.html
	updateCertificateV2Req := &hwapigmodel.UpdateCertificateV2Request{
		CertificateId: d.config.CertificateId,
		Body: &hwapigmodel.CertificateForm{
			Name:        fmt.Sprintf("certimate_%d", time.Now().UnixMilli()),
			CertContent: certPEM,
			PrivateKey:  privkeyPEM,
			Type: lo.
				If(showDetailsOfCertificateV2Resp.Type.Value() == hwapigmodel.GetCertificateFormTypeEnum().INSTANCE.Value(), lo.ToPtr(hwapigmodel.GetCertificateFormTypeEnum().INSTANCE)).
				Else(lo.ToPtr(hwapigmodel.GetCertificateFormTypeEnum().GLOBAL)),
			InstanceId: showDetailsOfCertificateV2Resp.InstanceId,
		},
	}
	updateCertificateV2Resp, err := d.sdkClient.UpdateCertificateV2(updateCertificateV2Req)
	d.logger.Debug("sdk request 'apig.UpdateCertificateV2'", slog.Any("request", updateCertificateV2Req), slog.Any("response", updateCertificateV2Resp))
	if err != nil {
		return fmt.Errorf("failed to execute sdk request 'apig.UpdateCertificateV2': %w", err)
	}

	return nil
}

func createSDKClient(accessKeyId, secretAccessKey, region string) (*hwapig.ApigClient, error) {
	projectId, err := getSDKProjectId(accessKeyId, secretAccessKey, region)
	if err != nil {
		return nil, err
	}

	auth, err := basic.NewCredentialsBuilder().
		WithAk(accessKeyId).
		WithSk(secretAccessKey).
		WithProjectId(projectId).
		SafeBuild()
	if err != nil {
		return nil, err
	}

	hcRegion, err := hwapigregion.SafeValueOf(region)
	if err != nil {
		return nil, err
	}

	hcClient, err := hwapig.ApigClientBuilder().
		WithRegion(hcRegion).
		WithCredential(auth).
		SafeBuild()
	if err != nil {
		return nil, err
	}

	client := hwapig.NewApigClient(hcClient)
	return client, nil
}

func getSDKProjectId(accessKeyId, secretAccessKey, region string) (string, error) {
	auth, err := global.NewCredentialsBuilder().
		WithAk(accessKeyId).
		WithSk(secretAccessKey).
		SafeBuild()
	if err != nil {
		return "", err
	}

	hcRegion, err := hwiamregion.SafeValueOf(region)
	if err != nil {
		return "", err
	}

	hcClient, err := hwiam.IamClientBuilder().
		WithRegion(hcRegion).
		WithCredential(auth).
		SafeBuild()
	if err != nil {
		return "", err
	}

	client := hwiam.NewIamClient(hcClient)

	request := &hwiammodel.KeystoneListProjectsRequest{
		Name: &region,
	}
	response, err := client.KeystoneListProjects(request)
	if err != nil {
		return "", err
	} else if response.Projects == nil || len(*response.Projects) == 0 {
		return "", fmt.Errorf("huaweicloud: no project found")
	}

	return (*response.Projects)[0].Id, nil
}
