// cmd/keygen — fm-rds-tx license key generator.
// KEEP PRIVATE: this tool contains the HMAC secret. Never distribute.
//
// Usage:
//   go run ./cmd/keygen free           → gratis key (no personal data)
//   go run ./cmd/keygen studio@wxy.fm  → commercial key for that station
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/jan/fm-rds-tx/internal/license"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Fprintf(os.Stderr, "usage: keygen <payload>\n")
		fmt.Fprintf(os.Stderr, "  payload: 'free' for gratis key, email for commercial\n")
		fmt.Fprintf(os.Stderr, "examples:\n")
		fmt.Fprintf(os.Stderr, "  keygen free\n")
		fmt.Fprintf(os.Stderr, "  keygen studio@example.fm\n")
		os.Exit(1)
	}

	payload := strings.TrimSpace(strings.Join(os.Args[1:], " "))
	if payload == "" {
		fmt.Fprintln(os.Stderr, "payload must not be empty")
		os.Exit(1)
	}

	key := license.GenerateKey(payload)
	fmt.Println(key)

	// Self-validate
	if !license.ValidateKey(key) {
		fmt.Fprintln(os.Stderr, "ERROR: generated key failed self-validation!")
		os.Exit(1)
	}
}
