// Yes Google do not work because I am in Europe and I can't get an API key...
// I can't dev, I will try using a VPN I guess at least so the features is available for people outside of Europe
package main

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

	"github.com/edgedb/edgedb-go"
	"github.com/gofiber/fiber/v2"
)

type GoogleRequestMessage struct {
	Role  string       `json:"role"`
	Parts []GooglePart `json:"parts"`
}

type GooglePart struct {
	Text string `json:"text"`
}

type GoogleChatCompletionRequest struct {
	Messages []GoogleRequestMessage `json:"contents"`
}

type GoogleChatCompletionResponse struct {
	Candidates    []GoogleCandidate   `json:"candidates"`
	UsageMetadata GoogleUsageMetadata `json:"usageMetadata"`
}

type GoogleCandidate struct {
	Content struct {
		Parts []GooglePart `json:"parts"`
		Role  string       `json:"role"`
	} `json:"content"`
	FinishReason  string               `json:"finishReason"`
	Index         int                  `json:"index"`
	SafetyRatings []GoogleSafetyRating `json:"safetyRatings"`
}

type GoogleSafetyRating struct {
	Category    string `json:"category"`
	Probability string `json:"probability"`
}

type GoogleUsageMetadata struct {
	PromptTokenCount     int32 `json:"promptTokenCount"`
	CandidatesTokenCount int32 `json:"candidatesTokenCount"`
	TotalTokenCount      int32 `json:"totalTokenCount"`
}

func addGoogleMessage(c *fiber.Ctx, llm LLM, selected bool) edgedb.UUID {
	Messages := getAllSelectedMessages(c)

	chatCompletion, err := RequestGoogle(c, llm.Model.ModelID, Messages, float64(llm.Temperature), llm.Context)
	if err != nil {
		fmt.Println("Error fetching user profile")
		panic(err)
	} else if len(chatCompletion.Candidates) == 0 {
		fmt.Println("No response from Google")
		id := insertBotMessage(c, "No response from Google", selected, llm.ID)
		return id
	} else {
		Content := chatCompletion.Candidates[0].Content.Parts[0].Text
		id := insertBotMessage(c, Content, selected, llm.ID)
		return id
	}
}

func TestGoogleKey(apiKey string) bool {
	url := "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=" + apiKey

	googlePart := GooglePart{
		Text: "Hello",
	}
	// Convert messages to OpenAI format
	googleMessages := []GoogleRequestMessage{
		{
			Role:  "user",
			Parts: []GooglePart{googlePart},
		},
	}

	requestBody := GoogleChatCompletionRequest{
		Messages: googleMessages,
	}

	jsonBody, err := json.Marshal(requestBody)
	if err != nil {
		fmt.Println("Error marshalling JSON: ", err)
		return false
	}

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

	req.Header.Set("Content-Type", "application/json")

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

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response body: ", err)
		return false
	}

	var chatCompletionResponse GoogleChatCompletionResponse
	err = json.Unmarshal(body, &chatCompletionResponse)
	if err != nil {
		fmt.Println("Error unmarshaling JSON: ", err)
		return false
	}
	if chatCompletionResponse.UsageMetadata.CandidatesTokenCount == 0 {
		fmt.Println("No response from Google")
		return false
	}

	fmt.Println("Response from Google: ", chatCompletionResponse)
	return true
}

func RequestGoogle(c *fiber.Ctx, model string, messages []Message, temperature float64, context string) (GoogleChatCompletionResponse, error) {
	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, "google")
	if err != nil {
		return GoogleChatCompletionResponse{}, fmt.Errorf("error getting Google API key: %w", err)
	}

	url := "https://generativelanguage.googleapis.com/v1beta/models/" + model + ":generateContent?key=" + apiKey

	// Do an array of GoogleRequestMessage with the messages in a for loop
	googleMessages := []GoogleRequestMessage{}
	for _, message := range messages {
		if message.Role == "user" {
			googleMessages = append(googleMessages, GoogleRequestMessage{
				Role:  "user",
				Parts: []GooglePart{{Text: message.Content}}, // Changed something here, to test
			})
		} else {
			googleMessages = append(googleMessages, GoogleRequestMessage{
				Role:  "model",
				Parts: []GooglePart{{Text: message.Content}},
			})
		}
	}

	requestBody := GoogleChatCompletionRequest{
		Messages: googleMessages,
	}

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

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

	req.Header.Set("Content-Type", "application/json")

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

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

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

	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 {
		return GoogleChatCompletionResponse{}, fmt.Errorf("error getting model info: %w", err)
	}

	var inputCost float32 = float32(chatCompletionResponse.UsageMetadata.PromptTokenCount) * usedModelInfo.InputPrice
	var outputCost float32 = float32(chatCompletionResponse.UsageMetadata.CandidatesTokenCount) * usedModelInfo.OutputPrice
	addUsage(c, inputCost, outputCost, chatCompletionResponse.UsageMetadata.PromptTokenCount, chatCompletionResponse.UsageMetadata.CandidatesTokenCount, model)

	return chatCompletionResponse, nil
}
