package dnsprovider

import (
	"context"
	"fmt"

	"github.com/libdns/cloudflare"
	"github.com/libdns/libdns"
)

// CloudflareProvider Cloudflare DNS 服务商实现
type CloudflareProvider struct {
	client *cloudflare.Provider
	domain string
}

// NewCloudflareProvider 创建 Cloudflare DNS 服务商实例
func NewCloudflareProvider(config ProviderConfig) (*CloudflareProvider, error) {
	if config.APIToken == "" {
		return nil, fmt.Errorf("API Token 不能为空")
	}

	provider := &cloudflare.Provider{
		APIToken: config.APIToken,
	}

	return &CloudflareProvider{
		client: provider,
		domain: config.Domain,
	}, nil
}

// AppendRecords 添加 DNS 记录
func (p *CloudflareProvider) AppendRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
	return p.client.AppendRecords(ctx, zone, recs)
}

// SetRecords 设置 DNS 记录（会覆盖现有记录）
func (p *CloudflareProvider) SetRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
	return p.client.SetRecords(ctx, zone, recs)
}

// GetRecords 获取 DNS 记录
func (p *CloudflareProvider) GetRecords(ctx context.Context, zone string) ([]libdns.Record, error) {
	return p.client.GetRecords(ctx, zone)
}

// DeleteRecords 删除 DNS 记录
func (p *CloudflareProvider) DeleteRecords(ctx context.Context, zone string, recs []libdns.Record) ([]libdns.Record, error) {
	return p.client.DeleteRecords(ctx, zone, recs)
}
