package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"message-service/commons/config"
	"message-service/commons/utils"
	"os"
	"strings"

	service "message-service/internal/service/central"
)

const (
	host                  = "http://127.0.0.1:10010"
	defaultConfigPath     = "/etc/dbaas/config.yaml"
	defaultTableInfosPath = "./table_info.json"
	defaultOutputPath     = "./table_info_with_question.json"
)

var exePath, _ = os.Executable()

type TableInfo struct {
	Database  string   `json:"database"`
	Table     string   `json:"table"`
	Sql       string   `json:"sql"`
	ColName   []string `json:"col_names"`
	Questions []string `json:"questions"`
}

func main() {
	var configPath string
	var tableInfosPath string
	var outputPath string
	flag.StringVar(&configPath, "config-path", defaultConfigPath, "DBaaS configuration file path")
	flag.StringVar(&tableInfosPath, "table-infos", defaultTableInfosPath, "DBaaS configuration file path")
	flag.StringVar(&outputPath, "output", defaultOutputPath, "")
	flag.Parse()
	err := config.InitConfig(configPath)
	if err != nil {
		panic(fmt.Sprintf("Initialize configuration failed, err: %+v", err))
	}
	tableInfos, err := ReadTableInfos(tableInfosPath)
	if err != nil {
		panic(fmt.Sprintf("ReadTableInfos failed, err: %+v , file is %s", err, tableInfosPath))
	}
	for index, tableInfo := range tableInfos {
		questionStr, err := GenQuestionsByCompletion(tableInfo.Sql)
		fmt.Println(questionStr)
		if err != nil {
			continue
		}
		questionList := strings.Split(questionStr, "\n")
		tableInfos[index].Table = strings.Replace(tableInfos[index].Table, "\n", " ", -1)
		tableInfos[index].Table = strings.Replace(tableInfos[index].Table, "\t", " ", -1)
		tableInfos[index].Questions = []string{}
		for _, question := range questionList {
			if question == "" {
				continue
			}
			tableInfos[index].Questions = append(tableInfos[index].Questions, question)
		}
	}
	outputStr, err := json.MarshalIndent(tableInfos, "", "\t")
	utils.WriteStringToFile(outputPath, string(outputStr))
}

func GenQuestionsByCompletion(tableInfo string) (string, error) {
	ctx := context.Background()
	prompt := `### MySQL table 

CREATE TABLE hacker_news (
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(200) NOT NULL,
  link VARCHAR(255) NOT NULL,
  points INT NOT NULL,
  author VARCHAR(100) NOT NULL,
  posted_date TIMESTAMP(6) NOT NULL,
  flag INT DEFAULT 0 NOT NULL,
  hide INT DEFAULT 0 NOT NULL,
  comments INT NOT NULL
)

### List interesting questions to ask about the data`
/*
	prompt := `### MySQL table
` + tableInfo + `
###
I'm a data analyst
Try to understand this table by table_name and column_name
We want to get some questions for transforming sql
The questions generated should be diverse
Guess the usage of this table
Give me different questions to find interesting data in this table
DO NOT generate questions about "average number"
###
think step by step, guess the usage of this table and generate 10 different questions to find interesting data, all questions are expressed using different sentences`
*/
	completion, err := service.CreateCompletionTextDavinci(ctx, prompt)
	if err != nil {
		return "", err
	}
	return completion, nil
}
func ReadTableInfos(tableInfoFile string) ([]TableInfo, error) {
	content, _ := utils.ReadFileToString(tableInfoFile)
	contentBytes := []byte(content)
	fmt.Println(content)
	tableInfos := make([]TableInfo, 0)
	err := json.Unmarshal(contentBytes, &tableInfos)
	if err != nil {
		return nil, err
	}
	return tableInfos, nil
}
