package main

import (
	"bytes"
	"encoding/csv"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"net/http"
	"os"
	"strings"
)

var (
	csvPath         = flag.String("csv", "aips.csv", "Path to the CSV file containing AIP names")
	elasticSearch   = flag.String("elastic", "http://127.0.0.1:62002", "elastic search URL")
	mysqlConnString = flag.String("mysql", "root:12345@tcp(localhost:62001)/MCP", "mysql connection string")
)

func main() {
	flag.Parse()
	file, err := os.Open(*csvPath)
	assertNoErr(err)
	records, err := csv.NewReader(file).ReadAll()
	assertNoErr(err)

	r := []string{}
	for _, record := range records {
		r = append(r, strings.TrimSpace(record[0]))
	}
	result, err := os.OpenFile("aip_names.txt", os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
	assertNoErr(err)

	// Query mapping to know if it's raw or keyword: /aips/_mapping
	q := struct {
		Query struct {
			Term struct {
				Name string `json:"name.raw"`
			} `json:"term"`
		} `json:"query"`
	}{}
	for _, name := range r {
		q.Query.Term.Name = name
		var res ElasticAipResponse
		err = do(*elasticSearch+"/aips/_search", q, &res)
		assertNoErr(err)
		if res.Hits.Total == 1 {
			fmt.Println("AIP Found: " + res.Hits.Hits[0].Source.Name)
			_, err = result.WriteString(name + "\n")
			assertNoErr(err)
		} else if res.Hits.Total > 1 {
			fmt.Println("more than one match, this is not expeted")
			fmt.Println("Name: ", name)
			os.Exit(1)
		}
	}
}

func do(path string, payload, responsePayload any) error {
	var body io.Reader
	if payload != nil {
		jsonBody, err := json.Marshal(payload)
		if err != nil {
			return err
		}
		body = bytes.NewReader(jsonBody)
	}
	req, err := http.NewRequest(http.MethodGet, path, body)
	if err != nil {
		return err
	}

	req.Header.Set("Content-Type", "application/json")
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}

	resBody, err := io.ReadAll(res.Body)
	if err != nil {
		return err
	}
	if res.StatusCode >= 400 {
		fmt.Println(string(resBody))
		return fmt.Errorf("request failed: %s", http.StatusText(res.StatusCode))
	}
	return json.Unmarshal(resBody, responsePayload)
}

func assertNoErr(err error) {
	if err != nil {
		panic(err)
	}
}
