package openai

import (
	"net/http"
	"net/url"
	"strings"

	"aigateway.local/core/internal/provider"
)

type Adapter struct {
	target *url.URL
	apiKey string
}

func New(baseURL, apiKey string) (*Adapter, error) {
	target, err := url.Parse(baseURL)
	if err != nil {
		return nil, err
	}
	return &Adapter{target: target, apiKey: apiKey}, nil
}

func (a *Adapter) Name() string { return "openai-compatible" }

func (a *Adapter) Target() *url.URL {
	copy := *a.target
	return &copy
}

func (a *Adapter) Capabilities() []provider.Capability {
	return []provider.Capability{
		provider.CapabilityChat,
		provider.CapabilityResponses,
		provider.CapabilityEmbeddings,
		provider.CapabilityMessages,
		provider.CapabilityModels,
	}
}

func (a *Adapter) Prepare(request *http.Request) {
	request.Header.Del("X-Gateway-API-Key")
	request.Header.Del("X-Gateway-Provider")
	basePath := strings.TrimRight(a.target.Path, "/")
	duplicatedVersionPrefix := basePath + "/v1/"
	if strings.HasSuffix(basePath, "/v1") && strings.HasPrefix(request.URL.Path, duplicatedVersionPrefix) {
		request.URL.Path = basePath + "/" + strings.TrimPrefix(request.URL.Path, duplicatedVersionPrefix)
		request.URL.RawPath = ""
	}
	if a.apiKey != "" {
		request.Header.Set("Authorization", "Bearer "+a.apiKey)
	}
}

var _ provider.Adapter = (*Adapter)(nil)
