package main

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

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

func RequestHuggingface(c *fiber.Ctx, llm LLM, messages []Message) string {
	url := llm.Endpoint.Endpoint
	temperature := float64(llm.Temperature)
	context := llm.Context
	maxTokens := int(llm.MaxToken)

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

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

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

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

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

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		logErrorCode.Println("10-01-0004")
		return "JADE internal error: 10-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("10-01-0005 -", resp.Status, "-", string(body))
		return "JADE internal error: 10-01-0005. Please contact the support."
	}

	addUsage(c, 0, 0, 0, 0, llm.Model.ModelID)

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