package dto

import (
	"github.com/ocdp/cluster-service/internal/domain/entity"
	"github.com/ocdp/cluster-service/internal/pkg/crypto"
)

// ToRegistryResponse 转换 Registry 实体为响应 DTO（脱敏）
func ToRegistryResponse(registry *entity.Registry) *RegistryResponse {
	response := &RegistryResponse{
		ID:          registry.ID,
		WorkspaceID: registry.WorkspaceID,
		OwnerID:     registry.OwnerID,
		Visibility:  registry.Visibility,
		Name:        registry.Name,
		URL:         registry.URL,
		Description: registry.Description,
		Username:    registry.Username,
		Insecure:    registry.Insecure,
		CreatedAt:   registry.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
		UpdatedAt:   registry.UpdatedAt.Format("2006-01-02T15:04:05Z07:00"),
	}

	// 脱敏处理密码
	if registry.Password != "" {
		response.HasPassword = true
		response.Password = crypto.MaskSensitiveData(registry.Password)
	}

	return response
}

// ToClusterResponse 转换 Cluster 实体为响应 DTO（脱敏）
func ToClusterResponse(cluster *entity.Cluster) *ClusterResponse {
	response := &ClusterResponse{
		ID:               cluster.ID,
		WorkspaceID:      cluster.WorkspaceID,
		OwnerID:          cluster.OwnerID,
		Visibility:       cluster.Visibility,
		Name:             cluster.Name,
		Host:             cluster.Host,
		Description:      cluster.Description,
		DefaultNamespace: cluster.DefaultNamespace,
		CreatedAt:        cluster.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
		UpdatedAt:        cluster.UpdatedAt.Format("2006-01-02T15:04:05Z07:00"),
	}

	// 设置认证配置状态标志
	response.HasCAData = cluster.CAData != ""
	response.HasCertData = cluster.CertData != ""
	response.HasKeyData = cluster.KeyData != ""
	response.HasToken = cluster.Token != ""

	// 脱敏处理敏感数据（仅显示掩码）
	if cluster.CAData != "" {
		response.CAData = crypto.MaskSensitiveData(cluster.CAData)
	}
	if cluster.CertData != "" {
		response.CertData = crypto.MaskSensitiveData(cluster.CertData)
	}
	if cluster.KeyData != "" {
		response.KeyData = crypto.MaskSensitiveData(cluster.KeyData)
	}
	if cluster.Token != "" {
		response.Token = crypto.MaskSensitiveData(cluster.Token)
	}

	return response
}
