//go:build generate
// +build generate

package main

import (
	"bytes"
	_ "embed"
	"encoding/csv"
	"fmt"
	"log"
	"os"
	"sort"
	"strings"
	"text/template"

	"github.com/hashicorp/terraform-provider-aws/names"
)

const (
	filename      = `../../../website/docs/guides/custom-service-endpoints.html.md`
	namesDataFile = "../../../names/names_data.csv"
)

//go:embed custom_endpoints_header.tmpl
var header string

//go:embed custom_endpoints_footer.tmpl
var footer string

type ServiceDatum struct {
	ProviderPackage string
	Aliases         []string
}

type TemplateData struct {
	Services []ServiceDatum
}

func main() {
	fmt.Printf("Generating %s\n", strings.TrimPrefix(filename, "../../../"))

	f, err := os.Open(namesDataFile)
	if err != nil {
		log.Fatal(err)
	}

	defer f.Close()

	csvReader := csv.NewReader(f)

	data, err := csvReader.ReadAll()
	if err != nil {
		log.Fatal(err)
	}

	td := TemplateData{}

	for i, l := range data {
		if i < 1 { // no header
			continue
		}

		if l[names.ColExclude] != "" {
			continue
		}

		if l[names.ColProviderPackageActual] == "" && l[names.ColProviderPackageCorrect] == "" {
			continue
		}

		p := l[names.ColProviderPackageCorrect]

		if l[names.ColProviderPackageActual] != "" {
			p = l[names.ColProviderPackageActual]
		}

		sd := ServiceDatum{
			ProviderPackage: p,
		}

		if l[names.ColAliases] != "" {
			sd.Aliases = strings.Split(l[names.ColAliases], ";")
		}

		td.Services = append(td.Services, sd)
	}

	sort.SliceStable(td.Services, func(i, j int) bool {
		return td.Services[i].ProviderPackage < td.Services[j].ProviderPackage
	})

	writeTemplate(header+tmpl+footer, "website", td)
}

func writeTemplate(body string, templateName string, td TemplateData) {
	// If the file doesn't exist, create it, or append to the file
	f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		log.Fatalf("error opening file (%s): %s", filename, err)
	}

	tplate, err := template.New(templateName).Parse(body)
	if err != nil {
		log.Fatalf("error parsing template: %s", err)
	}

	var buffer bytes.Buffer
	err = tplate.Execute(&buffer, td)
	if err != nil {
		log.Fatalf("error executing template: %s", err)
	}

	if _, err := f.Write(buffer.Bytes()); err != nil {
		f.Close()
		log.Fatalf("error writing to file (%s): %s", filename, err)
	}

	if err := f.Close(); err != nil {
		log.Fatalf("error closing file (%s): %s", filename, err)
	}
}

var tmpl = `
<!-- markdownlint-disable no-inline-html -->
<!--
    The division splits this long list into multiple columns without manually
    maintaining a table. The terraform.io Markdown parser previously allowed
    for Markdown within HTML elements, however the Terraform Registry parser
    is more accurate/strict, so we use raw HTML to maintain this list.
-->
<!-- Generated by internal/generate/customends/main.go; DO NOT EDIT. -->
<div style="column-width: 14em;">
<ul>
{{- range .Services }}
  {{- if .Aliases }}
  <li><code>{{ .ProviderPackage }}</code> ({{ range $i, $e := .Aliases }}{{ if gt $i 0 }} {{ end }}or <code>{{ $e }}</code>{{ end }})</li>
  {{- else }}
  <li><code>{{ .ProviderPackage }}</code></li>
  {{- end }}
{{- end }}
</ul>
</div>
<!-- markdownlint-enable no-inline-html -->
`
