package openai_compat

import (
	"net/http"
	"strings"

	"go.uber.org/zap"

	"iop/packages/go/config"
)

const Name = "openai_compat"

// Adapter is the OpenAI-compatible adapter. It carries an explicit provider
// label, per-request auth/header injection and top-level option passthrough so
// a provider boundary and its config contract stay distinct.
type Adapter struct {
	instanceName     string
	provider         string
	endpoint         string
	headers          map[string]string
	capacity         int
	maxQueue         int
	queueTimeoutMS   int
	requestTimeoutMS int
	client           *http.Client
	logger           *zap.Logger
}

// New creates an OpenAI-compatible adapter. The optional instanceName is the
// registry instance key used to disambiguate multiple instances on a node.
func New(cfg config.OpenAICompatConf, logger *zap.Logger, instanceName ...string) *Adapter {
	endpoint := strings.TrimRight(cfg.Endpoint, "/")
	name := Name
	if len(instanceName) > 0 && instanceName[0] != "" {
		name = instanceName[0]
	}
	var headers map[string]string
	if len(cfg.Headers) > 0 {
		headers = make(map[string]string, len(cfg.Headers))
		for k, v := range cfg.Headers {
			headers[k] = v
		}
	}
	return &Adapter{
		instanceName:     name,
		provider:         cfg.Provider,
		endpoint:         endpoint,
		headers:          headers,
		capacity:         cfg.Capacity,
		maxQueue:         cfg.MaxQueue,
		queueTimeoutMS:   cfg.QueueTimeoutMS,
		requestTimeoutMS: cfg.RequestTimeoutMS,
		client:           &http.Client{},
		logger:           logger,
	}
}

func (a *Adapter) Name() string { return Name }
