package main

import (
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"github.com/tidbcloud/dataservices/idl/pbgen/proto/dataapi"
	"message-service/commons/chatgpttemplate"
	"message-service/commons/config"
	"message-service/commons/utils"
	service "message-service/internal/service/central"
	"os"
	"strings"
	"sync"
)

const (
	defaultConfigPath     = "/etc/dbaas/config.yaml"
	defaultTableInfosPath = "./test/githubevent_auto_evaluation/sample_data/table_info_with_question.json"
	defaultOutputPath     = "./test/githubevent_auto_evaluation/sample_data//table_info_with_answer.json"
)

var wg sync.WaitGroup

var exePath, _ = os.Executable()

type TableInfo struct {
	Database  string   `json:"database"`
	Table     string   `json:"table"`
	ColNames  []string `json:"col_names"`
	Questions []string `json:"questions"`
	Answers   []string `json:"answers"`
}

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))
	}
	question := " create a weather report table and insert some sample data"
	//question = "what is the database name"
	database := "test"
	table := "hn"
	columns := "id, title, url, num_points, num_comments, author, created_at"
	split := strings.Split(columns, ", ")
	completion, err := GenSqlByCompletion(question, database, table, split)
	if err != nil {
		panic(fmt.Sprintf("GenSqlByCompletion failed, err: %+v", err))
	}
	fmt.Println(completion)
}

func GenAnswers(index int, tableInfosPtr *[]TableInfo) {
	tableInfos := *tableInfosPtr
	tableInfos[index].Answers = make([]string, len(tableInfos[index].Questions))
	for qIndex, question := range tableInfos[index].Questions {
		answer, err := GenSqlByCompletion(question, tableInfos[index].Database, tableInfos[index].Table, tableInfos[index].ColNames)
		if err != nil {
			continue
		}
		tableInfos[index].Answers[qIndex] = answer
	}
	//wg.Done()
}

func GenSqlByCompletion(question string, database, table string, colNames []string) (string, error) {
	ctx := context.Background()
	meta := dataapi.TableMeta{Table: table, Columns: colNames}
	metas := []*dataapi.TableMeta{&meta}
	formatPrompt := chatgpttemplate.FormatPromptForSqlEditorDatabase(question, database, metas)
	completion, err := service.CreateCompletionTextDavinci(ctx, formatPrompt)
	if err != nil {
		return "", err
	}
	return completion, nil
}
func ReadTableInfos(tableInfoFile string) ([]TableInfo, error) {
	content, _ := utils.ReadFileToString(tableInfoFile)
	contentBytes := []byte(content)
	tableInfos := make([]TableInfo, 0)
	err := json.Unmarshal(contentBytes, &tableInfos)
	if err != nil {
		return nil, err
	}
	return tableInfos, nil
}
