package uclouduclb

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

	"github.com/samber/lo"
	"github.com/ucloud/ucloud-sdk-go/services/ulb"
	"github.com/ucloud/ucloud-sdk-go/ucloud"
	"github.com/ucloud/ucloud-sdk-go/ucloud/auth"

	"github.com/certimate-go/certimate/pkg/core"
	cmgrimpl "github.com/certimate-go/certimate/pkg/core/certmgr/providers/ucloud-ulb"
	ucloudsdk "github.com/certimate-go/certimate/pkg/sdk3rd/ucloud/ulb"
	xloop "github.com/certimate-go/certimate/pkg/utils/loop"
)

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

type DeployerConfig struct {
	// 优刻得 API 私钥。
	PrivateKey string `json:"privateKey"`
	// 优刻得 API 公钥。
	PublicKey string `json:"publicKey"`
	// 优刻得项目 ID。
	ProjectId string `json:"projectId,omitempty"`
	// 优刻得地域。
	Region string `json:"region"`
	// 部署目标。
	DeployTarget string `json:"deployTarget"`
	// 负载均衡实例 ID。
	// 部署目标为 [DEPLOY_TARGET_LOADBALANCER]、[DEPLOY_TARGET_VSERVER] 时必填。
	LoadbalancerId string `json:"loadbalancerId,omitempty"`
	// 负载均衡 VServer ID。
	// 部署目标为 [DEPLOY_TARGET_VSERVER] 时必填。
	VServerId string `json:"vserverId,omitempty"`
}

type Deployer struct {
	config     *DeployerConfig
	logger     *slog.Logger
	sdkClient  *ucloudsdk.ULBClient
	sdkCertmgr core.Certmgr

	sslId2PemMap   map[string]string
	sslId2PemMapMu sync.Mutex
}

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.PrivateKey, config.PublicKey, config.ProjectId, config.Region)
	if err != nil {
		return nil, fmt.Errorf("could not create client: %w", err)
	}

	pcertmgr, err := cmgrimpl.NewCertmgr(&cmgrimpl.CertmgrConfig{
		PrivateKey: config.PrivateKey,
		PublicKey:  config.PublicKey,
		ProjectId:  config.ProjectId,
		Region:     config.Region,
	})
	if err != nil {
		return nil, fmt.Errorf("could not create certmgr: %w", err)
	}

	return &Deployer{
		config:     config,
		logger:     slog.Default(),
		sdkClient:  client,
		sdkCertmgr: pcertmgr,

		sslId2PemMap:   make(map[string]string),
		sslId2PemMapMu: sync.Mutex{},
	}, nil
}

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

	d.sdkCertmgr.SetLogger(logger)
}

func (d *Deployer) Deploy(ctx context.Context, certPEM, privkeyPEM string) (*DeployResult, error) {
	// 上传证书
	upres, err := d.sdkCertmgr.Upload(ctx, certPEM, privkeyPEM)
	if err != nil {
		return nil, fmt.Errorf("failed to upload certificate file: %w", err)
	} else {
		d.logger.Info("ssl certificate uploaded", slog.Any("result", upres))

		d.sslId2PemMapMu.Lock()
		d.sslId2PemMap[upres.CertId] = certPEM
		d.sslId2PemMapMu.Unlock()
	}

	// 根据部署目标决定业务流程
	switch d.config.DeployTarget {
	case DEPLOY_TARGET_LOADBALANCER:
		if err := d.deployToLoadbalancer(ctx, upres.CertId); err != nil {
			return nil, err
		}

	case DEPLOY_TARGET_VSERVER:
		if err := d.deployToVServer(ctx, upres.CertId); err != nil {
			return nil, err
		}

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

	return &DeployResult{}, nil
}

func (d *Deployer) deployToLoadbalancer(ctx context.Context, cloudCertId string) error {
	if d.config.LoadbalancerId == "" {
		return fmt.Errorf("config `loadbalancerId` is required")
	}

	// 获取 CLB 下的 HTTPS VServer 列表
	// REF: https://docs.ucloud.cn/api/ulb-api/describe_vserver
	vserverIds := make([]string, 0)
	describeVServerOffset := 0
	describeVServerLimit := 100
	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		default:
		}

		describeVServerReq := d.sdkClient.NewDescribeVServerRequest()
		describeVServerReq.ULBId = ucloud.String(d.config.LoadbalancerId)
		describeVServerReq.Offset = ucloud.Int(describeVServerOffset)
		describeVServerReq.Limit = ucloud.Int(describeVServerLimit)
		describeVServerResp, err := d.sdkClient.DescribeVServer(describeVServerReq)
		d.logger.Debug("sdk request 'ulb.DescribeVServer'", slog.Any("request", describeVServerReq), slog.Any("response", describeVServerResp))
		if err != nil {
			return fmt.Errorf("failed to execute sdk request 'ulb.DescribeVServer': %w", err)
		}

		for _, vserverItem := range describeVServerResp.DataSet {
			if vserverItem.Protocol == "HTTPS" {
				vserverIds = append(vserverIds, vserverItem.VServerId)
			}
		}

		if len(describeVServerResp.DataSet) < describeVServerLimit {
			break
		}

		describeVServerOffset += describeVServerLimit
	}

	// 批量更新 VServer 证书
	if len(vserverIds) == 0 {
		d.logger.Info("no clb vservers to deploy")
	} else {
		d.logger.Info("found clb vservers to deploy", slog.Any("vserverIds", vserverIds))

		if err := xloop.ForRangeAllWithContext(ctx, vserverIds, func(ctx context.Context, vserverId string, _ int) error {
			return d.updateVServerCertificate(ctx, d.config.LoadbalancerId, vserverId, cloudCertId)
		}); err != nil {
			return err
		}
	}

	return nil
}

func (d *Deployer) deployToVServer(ctx context.Context, cloudCertId string) error {
	if d.config.LoadbalancerId == "" {
		return fmt.Errorf("config `loadbalancerId` is required")
	}
	if d.config.VServerId == "" {
		return fmt.Errorf("config `vserverId` is required")
	}

	if err := d.updateVServerCertificate(ctx, d.config.LoadbalancerId, d.config.VServerId, cloudCertId); err != nil {
		return err
	}

	return nil
}

func (d *Deployer) updateVServerCertificate(ctx context.Context, cloudLoadbalancerId, cloudVServerId string, cloudCertId string) error {
	// 获取 CLB 下的 VServer 信息
	// REF: https://docs.ucloud.cn/api/ulb-api/describe_vserver
	describeVServerReq := d.sdkClient.NewDescribeVServerRequest()
	describeVServerReq.ULBId = ucloud.String(cloudLoadbalancerId)
	describeVServerReq.VServerId = ucloud.String(cloudVServerId)
	describeVServerReq.Limit = ucloud.Int(1)
	describeVServerResp, err := d.sdkClient.DescribeVServer(describeVServerReq)
	d.logger.Debug("sdk request 'ulb.DescribeVServer'", slog.Any("request", describeVServerReq), slog.Any("response", describeVServerResp))
	if err != nil {
		return fmt.Errorf("failed to execute sdk request 'ulb.DescribeVServer': %w", err)
	} else if len(describeVServerResp.DataSet) == 0 {
		return fmt.Errorf("could not find uclb vserver '%s'", cloudVServerId)
	}

	// 已部署过，直接返回
	vserverInfo := describeVServerResp.DataSet[0]
	if lo.SomeBy(vserverInfo.SSLSet, func(item ulb.ULBSSLSet) bool { return item.SSLId == cloudCertId }) {
		return nil
	}

	// 解绑 SSL 证书
	// REF: https://docs.ucloud.cn/api/ulb-api/unbind_ssl
	//
	// 注意，虽然文档中描述为数组结构，但实际 VServer 最多只允许绑定一个证书，因此需要先解绑旧证书才能绑定新证书
	// https://github.com/certimate-go/certimate/issues/1224
	for _, sslItem := range vserverInfo.SSLSet {
		unbindSSLReq := d.sdkClient.NewUnbindSSLRequest()
		unbindSSLReq.ULBId = ucloud.String(cloudLoadbalancerId)
		unbindSSLReq.VServerId = ucloud.String(cloudVServerId)
		unbindSSLReq.SSLId = ucloud.String(sslItem.SSLId)
		unbindSSLResp, err := d.sdkClient.UnbindSSL(unbindSSLReq)
		d.logger.Debug("sdk request 'ulb.UnbindSSL'", slog.Any("request", unbindSSLReq), slog.Any("response", unbindSSLResp))
		if err != nil {
			return fmt.Errorf("failed to execute sdk request 'ulb.UnbindSSL': %w", err)
		}
	}

	// 绑定 SSL 证书
	// REF: https://docs.ucloud.cn/api/ulb-api/bind_ssl
	bindSSLReq := d.sdkClient.NewBindSSLRequest()
	bindSSLReq.ULBId = ucloud.String(cloudLoadbalancerId)
	bindSSLReq.VServerId = ucloud.String(cloudVServerId)
	bindSSLReq.SSLId = ucloud.String(cloudCertId)
	bindSSLResp, err := d.sdkClient.BindSSL(bindSSLReq)
	d.logger.Debug("sdk request 'ulb.BindSSL'", slog.Any("request", bindSSLReq), slog.Any("response", bindSSLResp))
	if err != nil {
		return fmt.Errorf("failed to execute sdk request 'ulb.BindSSL': %w", err)
	}

	return nil
}

func createSDKClient(privateKey, publicKey, projectId, region string) (*ucloudsdk.ULBClient, error) {
	if privateKey == "" {
		return nil, fmt.Errorf("ucloud: invalid private key")
	}
	if publicKey == "" {
		return nil, fmt.Errorf("ucloud: invalid public key")
	}

	cfg := ucloud.NewConfig()
	cfg.ProjectId = projectId
	cfg.Region = region

	credential := auth.NewCredential()
	credential.PrivateKey = privateKey
	credential.PublicKey = publicKey

	client := ucloudsdk.NewClient(&cfg, &credential)
	return client, nil
}
