package main

import (
	"context"
	"flag"
	"fmt"
	"message-service/commons/config"

	gogpt "github.com/sashabaranov/go-gpt3"
)

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

func genQuestions(c *gogpt.Client) {
	ctx := context.Background()
	s := `
### MySQL table

CREATE TABLE "github_events" (
  "id" bigint(20) DEFAULT NULL,
  "type" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "created_at" datetime DEFAULT NULL,
  "repo_id" bigint(20) DEFAULT NULL,
  "repo_name" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "actor_id" bigint(20) DEFAULT NULL,
  "actor_login" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "actor_location" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "language" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "additions" bigint(20) DEFAULT NULL,
  "deletions" bigint(20) DEFAULT NULL,
  "action" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "number" int(11) DEFAULT NULL,
  "commit_id" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "comment_id" bigint(20) DEFAULT NULL,
  "org_login" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "org_id" bigint(20) DEFAULT NULL,
  "state" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "closed_at" datetime DEFAULT NULL,
  "comments" int(11) DEFAULT NULL,
  "pr_merged_at" datetime DEFAULT NULL,
  "pr_merged" tinyint(1) DEFAULT NULL,
  "pr_changed_files" int(11) DEFAULT NULL,
  "pr_review_comments" int(11) DEFAULT NULL,
  "pr_or_issue_id" bigint(20) DEFAULT NULL,
  "event_day" date DEFAULT NULL,
  "event_month" date DEFAULT NULL,
  "author_association" varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  "event_year" int(11) DEFAULT NULL,
  "push_size" int(11) DEFAULT NULL,
  "push_distinct_size" int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin

### List 50 interesting questions to ask about the data
`
	req := gogpt.CompletionRequest{
		Model:            "text-davinci-003",
		MaxTokens:        2048,
		Prompt:           s,
		Temperature:      0,
		Stop:             []string{"#", "-", ";", "\n\n"},
		TopP:             1,
		FrequencyPenalty: 0,
		PresencePenalty:  0,
		BestOf:           1,
	}
	resp, err := c.CreateCompletion(ctx, req)
	if err != nil {
		return
	}
	line := resp.Choices[0].Text
	println(line)
}
func testGenSql(c *gogpt.Client) {
	ctx := context.Background()
	s := `# MySQL SQL
---
---
The SQL need to be formated
only output SQL
If no SQL can be generated, output "No SQL Generated"
Generated-SQL need to end with ;
If the column name has spaces, wrap it with ` + "`" + `
---
# Question:
# let's think step by step, generate only one sql to do query: " create a weather report table and insert some sample data"
---`
	req := gogpt.CompletionRequest{
		Model:            "text-davinci-003",
		MaxTokens:        2048,
		Prompt:           s,
		Temperature:      0,
		Stop:             []string{"#", "-", ";", "\n\n"},
		TopP:             0.4,
		FrequencyPenalty: 0,
		PresencePenalty:  0,
		BestOf:           1,
	}
	resp, err := c.CreateCompletion(ctx, req)
	if err != nil {
		return
	}
	line := resp.Choices[0].Text
	println("=========== Question: ===============")
	println(s)
	println("=========== Answer: ===============")
	println(line)
}
func testGenSql1(c *gogpt.Client) {
	ctx := context.Background()
	s := `# MySQL SQL
---
---
The SQL need to be formated
only output SQL
If no SQL can be generated, output "No SQL Generated"
Generated-SQL need to end with ;
If the column name has spaces, wrap it with ` + "`" + `
---
# Question:
# let's think step by step, generate only one sql to do query: "What is best repo"
---`
	req := gogpt.CompletionRequest{
		Model:            "text-davinci-003",
		MaxTokens:        2048,
		Prompt:           s,
		Temperature:      0,
		Stop:             []string{"#", "-", ";", "\n\n"},
		TopP:             0.4,
		FrequencyPenalty: 0,
		PresencePenalty:  0,
		BestOf:           1,
	}
	resp, err := c.CreateCompletion(ctx, req)
	if err != nil {
		return
	}
	line := resp.Choices[0].Text
	println("=========== Question: ===============")
	println(s)
	println("=========== Answer: ===============")
	println(line)
}

func main() {
	var configPath string
	flag.StringVar(&configPath, "config-path", defaultConfigPath, "DBaaS configuration file path")
	flag.Parse()
	err := config.InitConfig(configPath)
	if err != nil {
		panic(fmt.Sprintf("Initialize configuration failed, err: %+v", err))
	}
	c := gogpt.NewClient(config.Global().MessageSvcConf.OpenAIApiKey)
	// genQuestions(c)
	testGenSql1(c)
	// testGenSql(c)
}
