package jdcloudalb

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

	jdcore "github.com/jdcloud-api/jdcloud-sdk-go/core"
	jdcommon "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
	jdlbapis "github.com/jdcloud-api/jdcloud-sdk-go/services/lb/apis"
	jdlbmodels "github.com/jdcloud-api/jdcloud-sdk-go/services/lb/models"
	"github.com/samber/lo"

	jdlb "github.com/certimate-go/certimate/pkg/sdk3rd-trimmed/github.com/jdcloud-api/jdcloud-sdk-go/services/lb/client"

	"github.com/certimate-go/certimate/pkg/core"
	cmgrimpl "github.com/certimate-go/certimate/pkg/core/certmgr/providers/jdcloud-ssl"
	xloop "github.com/certimate-go/certimate/pkg/utils/loop"
)

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

type DeployerConfig struct {
	// 京东云 AccessKeyId。
	AccessKeyId string `json:"accessKeyId"`
	// 京东云 AccessKeySecret。
	AccessKeySecret string `json:"accessKeySecret"`
	// 京东云地域 ID。
	RegionId string `json:"regionId"`
	// 部署目标。
	DeployTarget string `json:"deployTarget"`
	// 负载均衡器 ID。
	// 部署目标为 [DEPLOY_TARGET_LOADBALANCER] 时必填。
	LoadbalancerId string `json:"loadbalancerId,omitempty"`
	// 监听器 ID。
	// 部署目标为 [DEPLOY_TARGET_LISTENER] 时必填。
	ListenerId string `json:"listenerId,omitempty"`
	// SNI 域名（支持泛域名）。
	// 部署目标为 [DEPLOY_TARGET_LOADBALANCER]、[DEPLOY_TARGET_LISTENER] 时选填。
	Domain string `json:"domain,omitempty"`
}

type Deployer struct {
	config     *DeployerConfig
	logger     *slog.Logger
	sdkClient  *jdlb.LbClient
	sdkCertmgr core.Certmgr
}

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

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

	return &Deployer{
		config:     config,
		logger:     slog.Default(),
		sdkClient:  client,
		sdkCertmgr: pcertmgr,
	}, 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))
	}

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

	case DEPLOY_TARGET_LISTENER:
		if err := d.deployToListener(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")
	}

	// 查询负载均衡器详情
	// REF: https://docs.jdcloud.com/cn/load-balancer/api/describeloadbalancer
	describeLoadBalancerReq := jdlbapis.NewDescribeLoadBalancerRequestWithoutParam()
	describeLoadBalancerReq.SetRegionId(d.config.RegionId)
	describeLoadBalancerReq.SetLoadBalancerId(d.config.LoadbalancerId)
	describeLoadBalancerResp, err := d.sdkClient.DescribeLoadBalancer(describeLoadBalancerReq)
	d.logger.Debug("sdk request 'lb.DescribeLoadBalancer'", slog.Any("request", describeLoadBalancerReq), slog.Any("response", describeLoadBalancerResp))
	if err != nil {
		return fmt.Errorf("failed to execute sdk request 'lb.DescribeLoadBalancer': %w", err)
	}

	// 查询监听器列表
	// REF: https://docs.jdcloud.com/cn/load-balancer/api/describelisteners
	listenerIds := make([]string, 0)
	describeListenersPageNumber := 1
	describeListenersPageSize := 100
	for {
		select {
		case <-ctx.Done():
			return ctx.Err()
		default:
		}

		describeListenersReq := jdlbapis.NewDescribeListenersRequestWithoutParam()
		describeListenersReq.SetRegionId(d.config.RegionId)
		describeListenersReq.SetFilters([]jdcommon.Filter{{Name: "loadBalancerId", Values: []string{d.config.LoadbalancerId}}})
		describeListenersReq.SetPageSize(describeListenersPageNumber)
		describeListenersReq.SetPageSize(describeListenersPageSize)
		describeListenersResp, err := d.sdkClient.DescribeListeners(describeListenersReq)
		d.logger.Debug("sdk request 'lb.DescribeListeners'", slog.Any("request", describeListenersReq), slog.Any("response", describeListenersResp))
		if err != nil {
			return fmt.Errorf("failed to execute sdk request 'lb.DescribeListeners': %w", err)
		}

		for _, listener := range describeListenersResp.Result.Listeners {
			if strings.EqualFold(listener.Protocol, "https") || strings.EqualFold(listener.Protocol, "tls") {
				listenerIds = append(listenerIds, listener.ListenerId)
			}
		}

		if len(describeListenersResp.Result.Listeners) < describeListenersPageSize {
			break
		}

		describeListenersPageNumber++
	}

	// 批量更新监听器证书
	if len(listenerIds) == 0 {
		d.logger.Info("no alb listeners to deploy")
	} else {
		d.logger.Info("found alb listeners to deploy", slog.Any("listenerIds", listenerIds))

		if err := xloop.ForRangeAllWithContext(ctx, listenerIds, func(ctx context.Context, listenerId string, _ int) error {
			return d.updateListenerCertificate(ctx, listenerId, cloudCertId)
		}); err != nil {
			return err
		}
	}

	return nil
}

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

	// 更新监听器证书
	if err := d.updateListenerCertificate(ctx, d.config.ListenerId, cloudCertId); err != nil {
		return err
	}

	return nil
}

func (d *Deployer) updateListenerCertificate(ctx context.Context, cloudListenerId string, cloudCertId string) error {
	if d.config.Domain == "" {
		// 未指定 SNI，只需部署到监听器
		return d.updateListenerDefaultCertificate(ctx, cloudListenerId, cloudCertId)
	} else {
		// 指定 SNI，需部署到扩展证书
		return d.updateListenerSniCertificate(ctx, cloudListenerId, cloudCertId)
	}
}

func (d *Deployer) updateListenerDefaultCertificate(ctx context.Context, cloudListenerId string, cloudCertId string) error {
	// 修改监听器信息
	// REF: https://docs.jdcloud.com/cn/load-balancer/api/updatelistener
	updateListenerReq := jdlbapis.NewUpdateListenerRequestWithoutParam()
	updateListenerReq.SetRegionId(d.config.RegionId)
	updateListenerReq.SetListenerId(cloudListenerId)
	updateListenerReq.SetCertificateSpecs([]jdlbmodels.CertificateSpec{{CertificateId: cloudCertId}})
	updateListenerResp, err := d.sdkClient.UpdateListener(updateListenerReq)
	d.logger.Debug("sdk request 'lb.UpdateListener'", slog.Any("request", updateListenerReq), slog.Any("response", updateListenerResp))
	if err != nil {
		return fmt.Errorf("failed to execute sdk request 'lb.UpdateListener': %w", err)
	}

	return nil
}

func (d *Deployer) updateListenerSniCertificate(ctx context.Context, cloudListenerId string, cloudCertId string) error {
	// 查询监听器详情
	// REF: https://docs.jdcloud.com/cn/load-balancer/api/describelistener
	describeListenerReq := jdlbapis.NewDescribeListenerRequestWithoutParam()
	describeListenerReq.SetRegionId(d.config.RegionId)
	describeListenerReq.SetListenerId(cloudListenerId)
	describeListenerResp, err := d.sdkClient.DescribeListener(describeListenerReq)
	d.logger.Debug("sdk request 'lb.DescribeListener'", slog.Any("request", describeListenerReq), slog.Any("response", describeListenerResp))
	if err != nil {
		return fmt.Errorf("failed to execute sdk request 'lb.DescribeListener': %w", err)
	}

	// 如果不存在，则添加扩展证书
	// REF: https://docs.jdcloud.com/cn/load-balancer/api/addlistenercertificates
	extCertSpecs := lo.Filter(describeListenerResp.Result.Listener.ExtensionCertificateSpecs, func(extCertSpec jdlbmodels.ExtensionCertificateSpec, _ int) bool {
		return extCertSpec.Domain == d.config.Domain
	})
	if len(extCertSpecs) == 0 {
		addListenerCertificatesReq := jdlbapis.NewAddListenerCertificatesRequestWithoutParam()
		addListenerCertificatesReq.SetRegionId(d.config.RegionId)
		addListenerCertificatesReq.SetListenerId(cloudListenerId)
		addListenerCertificatesReq.SetCertificates([]jdlbmodels.ExtCertificateSpec{
			{
				CertificateId: cloudCertId,
				Domain:        d.config.Domain,
			},
		})
		addListenerCertificatesResp, err := d.sdkClient.AddListenerCertificates(addListenerCertificatesReq)
		d.logger.Debug("sdk request 'lb.AddListenerCertificates'", slog.Any("request", addListenerCertificatesReq), slog.Any("response", addListenerCertificatesResp))
		if err != nil {
			return fmt.Errorf("failed to execute sdk request 'lb.AddListenerCertificates': %w", err)
		}
	}

	// 批量修改扩展证书
	// REF: https://docs.jdcloud.com/cn/load-balancer/api/updatelistenercertificates
	updateListenerCertificatesReq := jdlbapis.NewUpdateListenerCertificatesRequestWithoutParam()
	updateListenerCertificatesReq.SetRegionId(d.config.RegionId)
	updateListenerCertificatesReq.SetListenerId(cloudListenerId)
	updateListenerCertificatesReq.SetCertificates(lo.Map(extCertSpecs, func(extCertSpec jdlbmodels.ExtensionCertificateSpec, _ int) jdlbmodels.ExtCertificateUpdateSpec {
		return jdlbmodels.ExtCertificateUpdateSpec{
			CertificateBindId: extCertSpec.CertificateBindId,
			CertificateId:     &cloudCertId,
			Domain:            &extCertSpec.Domain,
		}
	}))
	updateListenerCertificatesResp, err := d.sdkClient.UpdateListenerCertificates(updateListenerCertificatesReq)
	d.logger.Debug("sdk request 'lb.UpdateListenerCertificates'", slog.Any("request", updateListenerCertificatesReq), slog.Any("response", updateListenerCertificatesResp))
	if err != nil {
		return fmt.Errorf("failed to execute sdk request 'lb.UpdateListenerCertificates': %w", err)
	}

	return nil
}

func createSDKClient(accessKeyId, accessKeySecret string) (*jdlb.LbClient, error) {
	clientCredentials := jdcore.NewCredentials(accessKeyId, accessKeySecret)
	client := jdlb.NewLbClient(clientCredentials)
	client.DisableLogger()
	return client, nil
}
