package handler

import (
	"net/http"

	"git.zkcoi.com/zkcoi/meshray/internal/service"
	"github.com/gin-gonic/gin"
)

// DDNSHandler DDNS 配置处理器
type DDNSHandler struct {
	ddnsService *service.DDNSService
}

// NewDDNSHandler 创建 DDNS 处理器
func NewDDNSHandler(ddnsService *service.DDNSService) *DDNSHandler {
	return &DDNSHandler{
		ddnsService: ddnsService,
	}
}

// DDNSConfigRequest DDNS 配置请求
type DDNSConfigRequest struct {
	Provider        string `json:"provider"`
	AccessKeyID     string `json:"access_key_id"`
	AccessKeySecret string `json:"access_key_secret"`
	Domain          string `json:"domain"`
	TxtRecordName   string `json:"txt_record_name"`
	SyncMode        string `json:"sync_mode"`
	RetryInterval   int    `json:"retry_interval"`
	MaxRetries      int    `json:"max_retries"`
	Enabled         bool   `json:"enabled"`
}

// GetDDNSConfig 获取 DDNS 配置
// GET /api/v1/ddns/config
func (h *DDNSHandler) GetDDNSConfig(c *gin.Context) {
	config, err := h.ddnsService.GetConfig(c.Request.Context())
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"data": config,
	})
}

// UpdateDDNSConfig 更新 DDNS 配置
// PUT /api/v1/ddns/config
func (h *DDNSHandler) UpdateDDNSConfig(c *gin.Context) {
	var req DDNSConfigRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "参数错误：" + err.Error(),
		})
		return
	}

	if req.Provider == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "请选择 DNS 提供商",
		})
		return
	}
	if req.AccessKeyID == "" || req.AccessKeySecret == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "请输入完整的访问密钥",
		})
		return
	}
	if req.Domain == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "请输入域名",
		})
		return
	}
	if req.TxtRecordName == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "请输入 TXT 记录名称",
		})
		return
	}

	if err := h.ddnsService.UpdateConfig(c.Request.Context(), req); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "配置已保存",
	})
}

// TestDDNSRequest 测试请求
type TestDDNSRequest struct {
	Provider        string `json:"provider"`
	AccessKeyID     string `json:"access_key_id"`
	AccessKeySecret string `json:"access_key_secret"`
	Domain          string `json:"domain"`
	TxtRecordName   string `json:"txt_record_name"`
}

// TestDDNSConnectivity 测试 DDNS 连通性
// POST /api/v1/ddns/test
func (h *DDNSHandler) TestDDNSConnectivity(c *gin.Context) {
	var req TestDDNSRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "参数错误：" + err.Error(),
		})
		return
	}

	results := h.ddnsService.TestConnectivity(c.Request.Context(), req)

	c.JSON(http.StatusOK, gin.H{
		"data": gin.H{
			"results": results,
		},
	})
}

// SyncDDNS 手动同步 DDNS
// POST /api/v1/ddns/sync
func (h *DDNSHandler) SyncDDNS(c *gin.Context) {
	if err := h.ddnsService.SyncNow(c.Request.Context()); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"message": "同步成功",
	})
}
