package main

import (
	"bufio"
	"context"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"message-service/commons/config"
	"message-service/commons/utils"
	service "message-service/internal/service/central"
	"os"
	"strings"
)

const (
	defaultConfigPath = "/etc/dbaas/config.yaml"
)

//func main() {
//	var configPath string
//	var promptPath string
//	var ctx = context.Background()
//	flag.StringVar(&configPath, "config-path", defaultConfigPath, "DBaaS configuration file path")
//	flag.StringVar(&promptPath, "table-info-path", "./table-info-path", "DBaaS configuration file path")
//	flag.Parse()
//	err := config.InitConfig(configPath)
//	if err != nil {
//		panic(fmt.Sprintf("Initialize configuration failed, err: %+v", err))
//	}
//	prompt, err := utils.ReadFileToString(promptPath)
//	if err != nil {
//		panic(fmt.Sprintf("ReadFileToString failed, err: %+v", err))
//	}
//	fmt.Println("prompt is %+v", prompt)
//	resp, err := service.CreateCompletionTextDavinci(ctx, prompt)
//	fmt.Println("resp is %+v", resp)
//
//}

func main() {
	var configPath string
	var promptPath string
	flag.StringVar(&configPath, "config-path", defaultConfigPath, "DBaaS configuration file path")
	flag.StringVar(&promptPath, "cases-path", "./test/prompt_evaluation/repeat_case/col_with_space/cases", "DBaaS configuration file path")
	flag.Parse()
	err := config.InitConfig(configPath)
	if err != nil {
		panic(fmt.Sprintf("Initialize configuration failed, err: %+v", err))
	}
	files, err := ioutil.ReadDir(promptPath)
	if err != nil {
		panic(fmt.Sprintf("ReadDir failed, err: %+v", err))
	}

	txtOutFilePath := "./test/prompt_evaluation/repeat_case/col_with_space/cases/output_sumary.txt"
	txtOutFile, err := os.OpenFile(txtOutFilePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		panic(fmt.Sprintf("OpenFile out failed, err: %+v", err))
	}
	txtWriter := bufio.NewWriter(txtOutFile)

	var resps []CaseResp

	for i, f := range files {
		if strings.Contains(f.Name(), "output_sumary") {
			continue
		}
		index := fmt.Sprintf("***** %d *****************************", i)
		fileName := fmt.Sprintf("*** file: %s", f.Name())

		txtWriter.WriteString(index)
		txtWriter.WriteString("\n")
		txtWriter.WriteString(fileName)
		txtWriter.WriteString("\n")

		fmt.Println(index)
		fmt.Println(fileName)

		filePath := fmt.Sprintf("%s/%s", promptPath, f.Name())
		prompt, err := utils.ReadFileToString(filePath)
		if err != nil {
			panic(fmt.Sprintf("ReadFileToString failed, err: %+v", err))
		}
		promptContent := fmt.Sprintf("*** prompt: %s", prompt)
		txtWriter.WriteString(promptContent)
		txtWriter.WriteString("\n")

		resp, err := service.CreateCompletionTextDavinci(context.Background(), prompt)

		respContent := fmt.Sprintf("*** resp: %s", resp)
		txtWriter.WriteString(respContent)
		txtWriter.WriteString("\n\n\n")
		txtWriter.Flush()

		fmt.Println(respContent)
		fmt.Println()

		caseResp := CaseResp{
			Index:  i,
			File:   f.Name(),
			Prompt: prompt,
			Resp:   resp,
		}

		resps = append(resps, caseResp)
	}

	marshal, err := json.Marshal(resps)
	if err != nil {
		panic(fmt.Sprintf("Marshal resps failed, err: %+v", err))
	}

	jsonOutFilePath := "./test/prompt_evaluation/repeat_case/col_with_space/cases/output_sumary.json"
	jsonOutFile, err := os.OpenFile(jsonOutFilePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		panic(fmt.Sprintf("OpenFile out failed, err: %+v", err))
	}
	jsonWriter := bufio.NewWriter(jsonOutFile)

	jsonWriter.Write(marshal)
	jsonWriter.Flush()

}

type CaseResp struct {
	Index  int    `json:"index"`
	File   string `json:"file"`
	Prompt string `json:"prompt"`
	Resp   string `json:"resp"`
}
