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"
	defaulttableSqlPath = "./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 tableSqlPath string
	var outputPath string
	flag.StringVar(&configPath, "config-path", defaultConfigPath, "DBaaS configuration file path")
	flag.StringVar(&tableSqlPath, "table-sql", defaulttableSqlPath, "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))
	}
	sql, _ := utils.ReadFileToString(tableSqlPath)
	colStr := "id,type,created_at,repo_id,repo_name,actor_id,actor_login,actor_location,language,additions,deletions,action,number,commit_id,comment_id,org_login,org_id,state,closed_at,comments,pr_merged_at,pr_merged,pr_changed_files,pr_review_comments,pr_or_issue_id,event_day,event_month,author_association,event_year,push_size,push_distinct_size"
	tableInfo := TableInfo{
		Database: "gharchive_dev",
		Table:    "github_events",
		Sql:      sql,
		ColName:  strings.Split(colStr, ","),
	}
	tableInfos := []TableInfo{tableInfo}
	for index, tableInfo := range tableInfos {
		questionStr, err := GenQuestionsByCompletion(tableInfo.Sql)
		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
Table github_events, columns = [id, type, created_at, repo_id, repo_name, actor_id, actor_login, actor_location, language, additions, deletions, action, number, commit_id, comment_id, org_login, org_id, state, closed_at, comments, pr_merged_at, pr_merged, pr_changed_files, pr_review_comments, pr_or_issue_id, event_day, event_month, author_association, event_year, push_size, push_distinct_size]
###
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`
	// Think step by step, generate 10 questions for this table, do not use repetitive question sentences`
	fmt.Println(prompt)
	completion, err := service.CreateCompletionTextDavinci(ctx, prompt)
	if err != nil {
		return "", err
	}
	return completion, nil
}
