package internal

import (
	"context"
	"fmt"
	"sync"
	"time"

	"github.com/go-acme/lego/v5/challenge"
	"github.com/go-acme/lego/v5/challenge/dns01"
	"github.com/go-acme/lego/v5/platform/env"
	"github.com/samber/lo"
	"gitlab.ecloud.com/ecloud/ecloudsdkcloudcore/config"
	"gitlab.ecloud.com/ecloud/ecloudsdkclouddns/model"

	"github.com/certimate-go/certimate/pkg/sdk3rd-trimmed/gitlab.ecloud.com/ecloud/ecloudsdkclouddns"
)

const (
	envNamespace = "CMCCCLOUD_"

	EnvAccessKey = envNamespace + "ACCESS_KEY"
	EnvSecretKey = envNamespace + "SECRET_KEY"

	EnvTTL                = envNamespace + "TTL"
	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
	EnvReadTimeout        = envNamespace + "READ_TIMEOUT"
	EnvConnectTimeout     = envNamespace + "CONNECT_TIMEOUT"
)

var _ challenge.ProviderTimeout = (*DNSProvider)(nil)

type Config struct {
	AccessKey string
	SecretKey string

	TTL                int
	PropagationTimeout time.Duration
	PollingInterval    time.Duration
	ReadTimeout        int
	ConnectTimeout     int
}

type DNSProvider struct {
	config *Config
	client *ecloudsdkclouddns.Client

	recordIDs   map[string]string // Key: ChallengeToken; Value: RecordID
	recordIDsMu sync.Mutex
}

func NewDefaultConfig() *Config {
	return &Config{
		TTL:                env.GetOrDefaultInt(EnvTTL, 600),
		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 10*time.Minute),
		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
		ReadTimeout:        env.GetOrDefaultInt(EnvReadTimeout, 30),
		ConnectTimeout:     env.GetOrDefaultInt(EnvConnectTimeout, 30),
	}
}

func NewDNSProvider() (*DNSProvider, error) {
	values, err := env.Get(EnvAccessKey, EnvSecretKey)
	if err != nil {
		return nil, fmt.Errorf("cmccecloud: %w", err)
	}

	cfg := NewDefaultConfig()
	cfg.AccessKey = values[EnvAccessKey]
	cfg.SecretKey = values[EnvSecretKey]

	return NewDNSProviderConfig(cfg)
}

func NewDNSProviderConfig(cfg *Config) (*DNSProvider, error) {
	if cfg == nil {
		return nil, fmt.Errorf("cmccecloud: the configuration of the DNS provider is nil")
	}

	client := ecloudsdkclouddns.NewClient(&config.Config{
		AccessKey:      cfg.AccessKey,
		SecretKey:      cfg.SecretKey,
		PoolId:         "CIDC-CORE-00",
		ReadTimeOut:    cfg.ReadTimeout,
		ConnectTimeout: cfg.ConnectTimeout,
	})

	return &DNSProvider{
		config:      cfg,
		client:      client,
		recordIDs:   make(map[string]string),
		recordIDsMu: sync.Mutex{},
	}, nil
}

func (d *DNSProvider) Present(ctx context.Context, domain, token, keyAuth string) error {
	info := dns01.GetChallengeInfo(ctx, domain, keyAuth)

	zoneName, err := dns01.DefaultClient().FindZoneByFqdn(ctx, info.EffectiveFQDN)
	if err != nil {
		return fmt.Errorf("cmccecloud: could not find zone for domain %q: %w", domain, err)
	}

	subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, zoneName)
	if err != nil {
		return fmt.Errorf("cmccecloud: %w", err)
	}

	request := &model.CreateRecordOpenapiRequest{
		CreateRecordOpenapiBody: &model.CreateRecordOpenapiBody{
			LineId:      "0", // 默认线路
			Rr:          subDomain,
			DomainName:  dns01.UnFqdn(zoneName),
			Description: "certimate acme",
			Type:        model.CreateRecordOpenapiBodyTypeEnumTxt,
			Value:       info.Value,
			Ttl:         lo.ToPtr(int32(d.config.TTL)),
		},
	}
	response, err := d.client.CreateRecordOpenapi(request)
	if err != nil {
		return fmt.Errorf("cmccecloud: error when create record: %w", err)
	} else if response.State != model.CreateRecordOpenapiResponseStateEnumOk {
		return fmt.Errorf("cmccecloud: failed to create record: unexpected response state: '%s', errcode: '%s', errmsg: '%s'", response.State, response.ErrorCode, response.ErrorMessage)
	}

	d.recordIDsMu.Lock()
	d.recordIDs[token] = response.Body.RecordId
	d.recordIDsMu.Unlock()

	return nil
}

func (d *DNSProvider) CleanUp(ctx context.Context, domain, token, keyAuth string) error {
	info := dns01.GetChallengeInfo(ctx, domain, keyAuth)

	d.recordIDsMu.Lock()
	recordID, ok := d.recordIDs[token]
	d.recordIDsMu.Unlock()
	if !ok {
		return fmt.Errorf("cmccecloud: unknown record ID for '%s'", info.EffectiveFQDN)
	}

	request := &model.DeleteRecordOpenapiRequest{
		DeleteRecordOpenapiBody: &model.DeleteRecordOpenapiBody{
			RecordIdList: []string{recordID},
		},
	}
	response, err := d.client.DeleteRecordOpenapi(request)
	if err != nil {
		return fmt.Errorf("cmccecloud: error when delete record: %w", err)
	} else if response.State != model.DeleteRecordOpenapiResponseStateEnumOk {
		return fmt.Errorf("cmccecloud: failed to delete record, unexpected response state: '%s', errcode: '%s', errmsg: '%s'", response.State, response.ErrorCode, response.ErrorMessage)
	}

	d.recordIDsMu.Lock()
	delete(d.recordIDs, token)
	d.recordIDsMu.Unlock()

	return nil
}

func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
	return d.config.PropagationTimeout, d.config.PollingInterval
}
