//go:build ignore
// +build ignore

package main

import (
	"context"
	"log"
	"os"
	"strings"
	"text/template"

	"github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
)

var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
package okex

var spotSymbolMap = map[string]string{
{{- range $k, $v := .spot }}
	{{ printf "%q" $k }}: {{ printf "%q" $v }},
{{- end }}
}

var swapSymbolMap = map[string]string{
{{- range $k, $v := .swap }}
	{{ printf "%q" $k }}: {{ printf "%q" $v }},
{{- end }}
}

`))

func main() {
	ctx := context.Background()
	client := okexapi.NewClient()

	spotData, err := requestInstruments(ctx, client, okexapi.InstrumentTypeSpot)
	if err != nil {
		log.Fatal(err)
	}

	swapData, err := requestInstruments(ctx, client, okexapi.InstrumentTypeSwap)
	if err != nil {
		log.Fatal(err)
	}

	f, err := os.Create("symbols.go")
	if err != nil {
		log.Fatal(err)
	}

	defer f.Close()

	err = packageTemplate.Execute(f, map[string]map[string]string{"spot": spotData, "swap": swapData})
	if err != nil {
		log.Fatal(err)
	}
}

func requestInstruments(ctx context.Context, client *okexapi.RestClient, instType okexapi.InstrumentType) (map[string]string, error) {
	instruments, err := client.NewGetInstrumentsInfoRequest().InstType(instType).Do(ctx)
	if err != nil {
		return nil, err
	}

	data := make(map[string]string, len(instruments))
	for _, inst := range instruments {
		key := strings.ReplaceAll(inst.InstrumentID, "-", "")
		if instType == okexapi.InstrumentTypeSwap {
			key = strings.ReplaceAll(inst.InstrumentFamily, "-", "")
		}
		data[key] = inst.InstrumentID
	}
	return data, nil
}
