package conversion

import (
	"fmt"
	"sync"

	"nex/backend/internal/conversion/canonical"
)

// ProtocolAdapter 协议适配器接口
type ProtocolAdapter interface {
	ProtocolName() string
	ProtocolVersion() string
	SupportsPassthrough() bool

	DetectInterfaceType(nativePath string) InterfaceType
	BuildUrl(nativePath string, interfaceType InterfaceType) string
	BuildHeaders(provider *TargetProvider) map[string]string
	SupportsInterface(interfaceType InterfaceType) bool

	DecodeRequest(raw []byte) (*canonical.CanonicalRequest, error)
	EncodeRequest(req *canonical.CanonicalRequest, provider *TargetProvider) ([]byte, error)
	DecodeResponse(raw []byte) (*canonical.CanonicalResponse, error)
	EncodeResponse(resp *canonical.CanonicalResponse) ([]byte, error)

	CreateStreamDecoder() StreamDecoder
	CreateStreamEncoder() StreamEncoder

	EncodeError(err *ConversionError) ([]byte, int)

	DecodeModelsResponse(raw []byte) (*canonical.CanonicalModelList, error)
	EncodeModelsResponse(list *canonical.CanonicalModelList) ([]byte, error)
	DecodeModelInfoResponse(raw []byte) (*canonical.CanonicalModelInfo, error)
	EncodeModelInfoResponse(info *canonical.CanonicalModelInfo) ([]byte, error)
	DecodeEmbeddingRequest(raw []byte) (*canonical.CanonicalEmbeddingRequest, error)
	EncodeEmbeddingRequest(req *canonical.CanonicalEmbeddingRequest, provider *TargetProvider) ([]byte, error)
	DecodeEmbeddingResponse(raw []byte) (*canonical.CanonicalEmbeddingResponse, error)
	EncodeEmbeddingResponse(resp *canonical.CanonicalEmbeddingResponse) ([]byte, error)
	DecodeRerankRequest(raw []byte) (*canonical.CanonicalRerankRequest, error)
	EncodeRerankRequest(req *canonical.CanonicalRerankRequest, provider *TargetProvider) ([]byte, error)
	DecodeRerankResponse(raw []byte) (*canonical.CanonicalRerankResponse, error)
	EncodeRerankResponse(resp *canonical.CanonicalRerankResponse) ([]byte, error)

	// 统一模型 ID 相关方法
	ExtractUnifiedModelID(nativePath string) (string, error)
	ExtractModelName(body []byte, ifaceType InterfaceType) (string, error)
	RewriteRequestModelName(body []byte, newModel string, ifaceType InterfaceType) ([]byte, error)
	RewriteResponseModelName(body []byte, newModel string, ifaceType InterfaceType) ([]byte, error)
}

// AdapterRegistry 适配器注册表接口
type AdapterRegistry interface {
	Register(adapter ProtocolAdapter) error
	Get(protocolName string) (ProtocolAdapter, error)
	ListProtocols() []string
}

// memoryRegistry 基于内存的适配器注册表
type memoryRegistry struct {
	mu       sync.RWMutex
	adapters map[string]ProtocolAdapter
}

// NewMemoryRegistry 创建内存注册表
func NewMemoryRegistry() AdapterRegistry {
	return &memoryRegistry{
		adapters: make(map[string]ProtocolAdapter),
	}
}

// Register 注册适配器
func (r *memoryRegistry) Register(adapter ProtocolAdapter) error {
	r.mu.Lock()
	defer r.mu.Unlock()

	name := adapter.ProtocolName()
	if _, exists := r.adapters[name]; exists {
		return fmt.Errorf("适配器已注册: %s", name)
	}
	r.adapters[name] = adapter
	return nil
}

// Get 获取适配器
func (r *memoryRegistry) Get(protocolName string) (ProtocolAdapter, error) {
	r.mu.RLock()
	defer r.mu.RUnlock()

	adapter, ok := r.adapters[protocolName]
	if !ok {
		return nil, fmt.Errorf("未找到适配器: %s", protocolName)
	}
	return adapter, nil
}

// ListProtocols 列出所有已注册协议
func (r *memoryRegistry) ListProtocols() []string {
	r.mu.RLock()
	defer r.mu.RUnlock()

	protocols := make([]string, 0, len(r.adapters))
	for name := range r.adapters {
		protocols = append(protocols, name)
	}
	return protocols
}
