package main

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

	"github.com/edgedb/edgedb-go"
)

type GroqChatCompletionRequest struct {
	Model       string           `json:"model"`
	Messages    []RequestMessage `json:"messages"`
	Temperature float64          `json:"temperature"`
}

type GroqChatCompletionResponse struct {
	ID      string       `json:"id"`
	Object  string       `json:"object"`
	Created int64        `json:"created"`
	Model   string       `json:"model"`
	Usage   GroqUsage    `json:"usage"`
	Choices []GroqChoice `json:"choices"`
}

type GroqUsage struct {
	PromptTokens     int32 `json:"prompt_tokens"`
	CompletionTokens int32 `json:"completion_tokens"`
	TotalTokens      int32 `json:"total_tokens"`
}

type GroqChoice struct {
	Message      Message `json:"message"`
	FinishReason string  `json:"finish_reason"`
	Index        int     `json:"index"`
}

func addGroqMessage(llm LLM, selected bool) edgedb.UUID {
	Messages := getAllSelectedMessages()

	chatCompletion, err := RequestGroq(llm.Model.ModelID, Messages, float64(llm.Temperature))
	if err != nil {
		panic(err)
	} else if len(chatCompletion.Choices) == 0 {
		fmt.Println("No response from Groq")
		id := insertBotMessage("No response from Groq", selected, llm.ID)
		return id
	} else {
		Content := chatCompletion.Choices[0].Message.Content
		id := insertBotMessage(Content, selected, llm.ID)
		return id
	}
}

func TestGroqKey(apiKey string) bool {
	url := "https://api.groq.com/openai/v1/chat/completions"

	// Convert messages to Qroq format
	groqMessages := []Message{
		{
			Role:    "user",
			Content: "Hello",
		},
	}

	requestBody := GroqChatCompletionRequest{
		Model:       "llama3-8b-8192",
		Messages:    Message2RequestMessage(groqMessages),
		Temperature: 0,
	}

	jsonBody, err := json.Marshal(requestBody)
	if err != nil {
		return false
	}

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
	if err != nil {
		return false
	}

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

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return false
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return false
	}

	var chatCompletionResponse GroqChatCompletionResponse
	err = json.Unmarshal(body, &chatCompletionResponse)
	if err != nil {
		return false
	}
	if chatCompletionResponse.Usage.CompletionTokens == 0 {
		return false
	}
	return true
}

func RequestGroq(model string, messages []Message, temperature float64) (GroqChatCompletionResponse, error) {
	var apiKey string
	err := edgeClient.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, "groq")
	if err != nil {
		return GroqChatCompletionResponse{}, fmt.Errorf("error getting Groq API key: %w", err)
	}

	url := "https://api.groq.com/openai/v1/chat/completions"

	requestBody := GroqChatCompletionRequest{
		Model:       model,
		Messages:    Message2RequestMessage(messages),
		Temperature: temperature,
	}

	jsonBody, err := json.Marshal(requestBody)
	if err != nil {
		return GroqChatCompletionResponse{}, fmt.Errorf("error marshaling JSON: %w", err)
	}

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
	if err != nil {
		return GroqChatCompletionResponse{}, fmt.Errorf("error creating request: %w", err)
	}

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

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		return GroqChatCompletionResponse{}, fmt.Errorf("error sending request: %w", err)
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return GroqChatCompletionResponse{}, fmt.Errorf("error reading response body: %w", err)
	}

	var chatCompletionResponse GroqChatCompletionResponse
	err = json.Unmarshal(body, &chatCompletionResponse)
	if err != nil {
		return GroqChatCompletionResponse{}, fmt.Errorf("error unmarshaling JSON: %w", err)
	}

	var usedModelInfo ModelInfo
	edgeClient.QuerySingle(edgeCtx, `
		Select ModelInfo {
			inputPrice,
			outputPrice
		}
		Filter ModelInfo.model = <str>$0
	`, &usedModelInfo, model)

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

	return chatCompletionResponse, nil
}
