package main

import (
	"encoding/csv"
	"io"
	"log"
	"os"

	"github.com/meilisearch/meilisearch-go"
)

func search() {
	// client := newClient()
	// _, err := client.Index("addressex").UpdateIndex()
	// if err != nil {
	// 	panic(err)
	// }
}

func init() {
	// TODO: (jpd) move this logic to their own endopints
	createIndex()
	updateIndex()
	loadAddresses()
}

func createIndex() {
	indexConfig := &meilisearch.IndexConfig{Uid: "addressex", PrimaryKey: "id"}
	client := newClient()
	_, err := client.CreateIndex(indexConfig)
	if err != nil {
		panic(err)
	}
}

func updateIndex() {
	index := newClientIndex()
	_, err := index.UpdateSettings(&meilisearch.Settings{
		DisplayedAttributes: []string{
			"id",
			"country",
			"state",
			"city",
			"neighborhood",
			"road",
			"house_number",
			"house",
			"unit",
			"postal_code",
			"raw_address",
		},
		FilterableAttributes: []string{
			"country",
			"state",
			"city",
			"neighborhood",
			"road",
			"house_number",
			"house",
			"unit",
			"postal_code",
		},
		SearchableAttributes: []string{
			"house_number",
			"house",
			"unit",
			"postal_code",
		},
		Synonyms: map[string][]string{
			"apartamento": []string{"apto", "apt", "ap"},
			"bloco":       []string{"bl", "b"},
			"torre":       []string{"tr", "t"},
			"conjunto":    []string{"conj", "cj"},
		},
	})
	if err != nil {
		panic(err)
	}
}

func loadAddresses() {
	// TODO: (jpd) use env var to set address file
	f, err := os.Open("/opt/src/config/data/addresses.csv")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	index := newClientIndex()

	rows := csv.NewReader(f)
	data := []Address{}
	for {
		row, err := rows.Read()
		if err == io.EOF {
			break
		} else if err != nil {
			panic(err)
		}
		rawAddress := row[0]
		data = append(data, parseAddress(rawAddress))
		if len(data) == 1000 {
			log.Printf("Recorded %d addresses", len(data))
			_, err := index.AddDocuments(data, "id")
			if err != nil {
				panic(err)
			}
			data = []Address{}
		}
	}
	log.Printf("Recorded %d addresses", len(data))
	_, err = index.AddDocuments(data, "id")
	if err != nil {
		panic(err)
	}
}

func newClientIndex() meilisearch.IndexManager {
	client := newClient()
	return client.Index("addressex")
}

func newClient() meilisearch.ServiceManager {
	return meilisearch.New("http://meili:7700", meilisearch.WithAPIKey(os.Getenv("MEILI_MASTER_KEY")))
}
