// beeper-imessage - A Matrix-iMessage puppeting bridge.
// Copyright (C) 2023 Beeper, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//go:build ignore

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
)

type ErrorInfo struct {
	Code int    `json:"code"`
	Name string `json:"name"`
}

func main() {
	f, err := os.Open("errors.json")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	var errors []ErrorInfo
	if err := json.NewDecoder(f).Decode(&errors); err != nil {
		panic(err)
	}

	var constString strings.Builder
	var errorSwitchString strings.Builder
	for _, e := range errors {
		constString.WriteString(fmt.Sprintf("IDSStatus%s IDSStatus = %d;", e.Name, e.Code))
		errorSwitchString.WriteString(fmt.Sprintf("case IDSStatus%s:return \"%s\";", e.Name, e.Name))
	}

	outfile, err := os.Create("idserrors.gen.go")
	if err != nil {
		panic(err)
	}

	outfile.WriteString(fmt.Sprintf(`
		package types

		// ========== WARNING ==========
		//
		// This file is auto-generated by generator.go. Do not edit!
		//
		// Instead, edit errors.json and run generator.go.
		//
		// errors.json was created using the data found in the JavaScript file for web
		// FaceTime.
		//
		// ==============================

		import "fmt"

		const (%s)

		func (s IDSStatus) String() string {
			switch s {%s
			default:
					return fmt.Sprintf("IDSStatus(%%d)", int(s))
			}
		}`,
		constString.String(),
		errorSwitchString.String(),
	))
}
