package main

import (
	"bytes"
	"encoding/json"
	"io"
	"net/http"
	"strings"

	"github.com/gofiber/fiber/v2"
)

func RequestNim(c *fiber.Ctx, llm LLM, messages []Message) string {
	model := llm.Model.ModelID
	temperature := float64(llm.Temperature)
	context := llm.Context
	maxTokens := int(llm.MaxToken)

	var apiKey string
	err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, `
		with
			filtered_keys := (
				select Key {
					key
				} filter .company.name = <str>$0 AND .<keys[is Setting].<setting[is User] = global currentUser
			)
		select filtered_keys.key limit 1
	`, &apiKey, "nim")
	if err != nil {
		logErrorCode.Println("05-00-0000")
		return "JADE internal error: 05-00-0000. Please contact the support."
	}

	url := "https://integrate.api.nvidia.com/v1/chat/completions"

	requestBody := OpenaiChatCompletionRequest{
		Model:       model,
		Messages:    Message2RequestMessage(messages, context),
		MaxTokens:   maxTokens,
		Temperature: temperature,
	}

	jsonBody, err := json.Marshal(requestBody)
	if err != nil {
		logErrorCode.Println("05-01-0001")
		return "JADE internal error: 05-01-0001. Please contact the support."
	}

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
	if err != nil {
		logErrorCode.Println("05-02-0002")
		return "JADE internal error: 05-02-0002. Please contact the support."
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Authorization", "Bearer "+apiKey)

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		logErrorCode.Println("05-02-0003")
		return "JADE internal error: 05-02-0003. Please contact the support."
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		logErrorCode.Println("05-01-0004")
		return "JADE internal error: 05-01-0004. Please contact the support."
	}

	for key, value := range OpenaiErrorCodes {
		if strings.Contains(resp.Status, key) {
			return value
		}
	}

	var chatCompletionResponse OpenaiChatCompletionResponse
	err = json.Unmarshal(body, &chatCompletionResponse)
	if err != nil {
		logErrorCode.Println("05-01-0005")
		return "JADE internal error: 05-01-0005. Please contact the support."
	}

	var usedModelInfo ModelInfo
	err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, `
		SELECT ModelInfo {
			inputPrice,
			outputPrice
		}
		FILTER .modelID = <str>$0
		LIMIT 1
	`, &usedModelInfo, model)
	if err != nil {
		logErrorCode.Println("05-00-0006")
		return "JADE internal error: 05-00-0006. Please contact the support."
	}

	var inputCost float32 = float32(chatCompletionResponse.Usage.PromptTokens) * usedModelInfo.InputPrice
	var outputCost float32 = float32(chatCompletionResponse.Usage.CompletionTokens) * usedModelInfo.OutputPrice
	addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)

	if len(chatCompletionResponse.Choices) == 0 {
		logErrorCode.Println("05-03-0007 -", resp.Status, "-", string(body))
		return "JADE internal error: 05-03-0007. Please contact the support."
	}

	return chatCompletionResponse.Choices[0].Message.Content
}
