package service

import (
	"encoding/json"
	"time"
)

// EmbeddingRequest represents a request for embeddings
type EmbeddingRequest struct {
	Model     string         `json:"model" binding:"required"`
	Texts     []string       `json:"texts" binding:"required"`
	OtherOpts map[string]any `json:"-"` // Will be populated from the raw JSON
}

// UnmarshalJSON custom unmarshaling to capture all additional options
func (r *EmbeddingRequest) UnmarshalJSON(data []byte) error {
	// First unmarshal into a generic map
	var raw map[string]any
	if err := json.Unmarshal(data, &raw); err != nil {
		return err
	}

	// Extract required fields
	if model, ok := raw["model"].(string); ok {
		r.Model = model
	}

	if texts, ok := raw["texts"].([]interface{}); ok {
		r.Texts = make([]string, len(texts))
		for i, text := range texts {
			if str, ok := text.(string); ok {
				r.Texts[i] = str
			}
		}
	}

	// Store all other options
	r.OtherOpts = make(map[string]any)
	for key, value := range raw {
		if key != "model" && key != "texts" {
			r.OtherOpts[key] = value
		}
	}

	return nil
}

// EmbeddingResponse represents the response for embedding requests
type EmbeddingResponse struct {
	Embeddings [][]byte           `json:"embeddings" swaggertype:"array,string"` // The actual embeddings are returned as base64 strings for each text
	Usage      EmbeddingUsageInfo `json:"usage"`
}

// EmbeddingUsageInfo represents usage information in the response
type EmbeddingUsageInfo struct {
	PromptTokens int `json:"prompt_tokens"`
	TotalTokens  int `json:"total_tokens"`
}

// QuotaUsageResponse represents quota usage for a tenant
type QuotaUsageResponse struct {
	TenantID     string    `json:"tenant_id"`
	CurrentUsage float64   `json:"current_usage"`
	QuotaLimit   float64   `json:"quota_limit"`
	ResetDate    time.Time `json:"reset_date"`
}

// AllQuotaUsageResponse represents quota usage for all tenants
type AllQuotaUsageResponse struct {
	Tenants []QuotaUsageResponse `json:"tenants"`
}

// ErrorResponse represents an error response
type ErrorResponse struct {
	Error string `json:"error"`
}
