package openai

import (
	"encoding/json"
)

// Responses API types

type responsesRequest struct {
	Model           string          `json:"model"`
	Input           json.RawMessage `json:"input"`
	Instructions    string          `json:"instructions,omitempty"`
	Stream          bool            `json:"stream"`
	Background      bool            `json:"background,omitempty"`
	Metadata        json.RawMessage `json:"metadata,omitempty"`
	MaxOutputTokens *int            `json:"max_output_tokens,omitempty"`
	Temperature     *float64        `json:"temperature,omitempty"`
	TopP            *float64        `json:"top_p,omitempty"`
}

// responsesResumeRequest is the private, endpoint-native continuation shape
// emitted by buildOpenAIResponsesResumeBody. It is deliberately separate from
// responsesRequest: public normalized ingress continues to accept string input
// only, while a recovery attempt may carry the already-validated assistant
// output items needed to resume an aborted provider attempt.
type responsesResumeRequest struct {
	Request   responsesRequest
	Content   string
	Reasoning string
}

// responsesEnvelope holds the routing-relevant fields decoded leniently from a
// /v1/responses request body before strict normalization. Unknown fields are
// ignored so the provider tunnel passthrough can forward Codex/Responses
// payloads verbatim.
type responsesEnvelope struct {
	Model      string          `json:"model"`
	Metadata   json.RawMessage `json:"metadata,omitempty"`
	Stream     bool            `json:"stream"`
	Background bool            `json:"background,omitempty"`
}

func (req responsesRequest) providerOptions() map[string]any {
	options := map[string]any{}
	if req.MaxOutputTokens != nil {
		options["max_tokens"] = *req.MaxOutputTokens
	}
	setOptionFloat(options, "temperature", req.Temperature)
	setOptionFloat(options, "top_p", req.TopP)
	return options
}

type responsesResponse struct {
	ID         string                `json:"id"`
	Object     string                `json:"object"`
	CreatedAt  int64                 `json:"created_at"`
	Model      string                `json:"model"`
	OutputText string                `json:"output_text"`
	Output     []responsesOutputItem `json:"output"`
	Usage      openAIUsage           `json:"usage"`
}

type responsesOutputItem struct {
	Type      string                 `json:"type"`
	Role      string                 `json:"role,omitempty"`
	Content   []responsesContentItem `json:"content,omitempty"`
	ID        string                 `json:"id,omitempty"`
	CallID    string                 `json:"call_id,omitempty"`
	Name      string                 `json:"name,omitempty"`
	Arguments string                 `json:"arguments,omitempty"`
}

type responsesContentItem struct {
	Type string `json:"type"`
	Text string `json:"text"`
}
