package main

import (
	"encoding/json"
	"fmt"
	"strconv"

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

// LLM stuff
func deleteLLM(c *fiber.Ctx) error {
	var selectedLLMIds []string
	err := json.Unmarshal([]byte(c.FormValue("selectedLLMIds")), &selectedLLMIds)
	if err != nil {
		fmt.Println("Error unmarshalling selected LLM IDs")
		panic(err)
	}

	for _, id := range selectedLLMIds {
		idUUID, _ := edgedb.ParseUUID(id)
		err := edgeClient.Execute(edgeCtx, `
			UPDATE LLM
			FILTER .id = <uuid>$0 AND .user = global currentUser
			SET {
				to_delete := true
			};
		`, idUUID)
		if err != nil {
			fmt.Println("Error deleting LLM")
			panic(err)
		}
	}

	deleteLLMtoDelete()

	return c.SendString(GenerateModelPopoverHTML(true))
}

func deleteLLMtoDelete() {
	err := edgeClient.Execute(edgeCtx, `
	delete LLM
	filter .to_delete = true and not exists(
		select Message filter .llm = LLM
	);
	`)
	if err != nil {
		panic(err)
	}
}

func createLLM(c *fiber.Ctx) error {
	name := c.FormValue("model-name-input")
	modelID := c.FormValue("selectedLLMId")
	temperature := c.FormValue("temperature-slider")
	f, _ := strconv.ParseFloat(temperature, 32)
	temperatureFloat := float32(f)

	systemPrompt := c.FormValue("model-prompt-input")
	url := c.FormValue("model-url-input")
	token := c.FormValue("model-key-input")
	customID := c.FormValue("model-cid-input")

	if modelID == "custom" {
		err := edgeClient.Execute(edgeCtx, `
		WITH
			countLLM := count((SELECT LLM FILTER .user = global currentUser))
		INSERT LLM {
			name := <str>$0,
			context := <str>$1,
			temperature := <float32>$2,
			position := countLLM + 1,
			modelInfo := (INSERT ModelInfo {
				name := <str>$0,
				modelID := <str>$5,
				inputPrice := 0.0,
				outputPrice := 0.0,
				maxToken := 500,
				company := (SELECT Company FILTER .name = "huggingface" LIMIT 1),
			}),
			custom_endpoint := (INSERT CustomEndpoint {
				endpoint := <str>$3,
				key := <str>$4,
			}),
			user := global currentUser
		};
		`, name, systemPrompt, temperatureFloat, url, token, customID) // TODO Add real max token
		if err != nil {
			fmt.Println("Error creating LLM")
			panic(err)
		}
	} else {
		err := edgeClient.Execute(edgeCtx, `
		WITH
			countLLM := count((SELECT LLM FILTER .user = global currentUser))
		INSERT LLM {
			name := <str>$0,
			context := <str>$1,
			temperature := <float32>$2,
			position := countLLM + 1,
			modelInfo := (SELECT ModelInfo FILTER .modelID = <str>$3 LIMIT 1),
			user := global currentUser
		}
	`, name, systemPrompt, temperatureFloat, modelID)
		if err != nil {
			fmt.Println("Error creating LLM")
			panic(err)
		}
	}

	return c.SendString(GenerateModelPopoverHTML(true))
}

type PositionUpdate struct {
	Position int    `json:"position"`
	ID       string `json:"id"`
}

func updateLLMPositionBatch(c *fiber.Ctx) error {
	var positionUpdates []PositionUpdate
	if err := c.BodyParser(&positionUpdates); err != nil {
		return err
	}

	for _, update := range positionUpdates {
		idUUID, err := edgedb.ParseUUID(update.ID)
		if err != nil {
			fmt.Println("Error parsing UUID")
			panic(err)
		}

		err = edgeClient.Execute(edgeCtx, `
			UPDATE LLM
			FILTER .id = <uuid>$0 AND .user = global currentUser
			SET {
				position := <int32>$1
			};
		`, idUUID, int32(update.Position))
		if err != nil {
			fmt.Println("Error updating LLM position")
			panic(err)
		}
	}

	return nil
}

// When we reorder the LLM list
func updateConversationPositionBatch(c *fiber.Ctx) error {
	var positionUpdates []PositionUpdate
	if err := c.BodyParser(&positionUpdates); err != nil {
		return err
	}

	for _, update := range positionUpdates {
		fmt.Println(update.ID)
		idUUID, err := edgedb.ParseUUID(update.ID)
		if err != nil {
			fmt.Println("Error parsing UUID")
			panic(err)
		}

		err = edgeClient.Execute(edgeCtx, `
			UPDATE Conversation
			FILTER .id = <uuid>$0 AND .user = global currentUser
			SET {
				position := <int32>$1
			};
		`, idUUID, int32(update.Position))
		if err != nil {
			fmt.Println("Error updating conversation position")
			panic(err)
		}
	}

	return nil
}
