package changelog

import (
	_ "embed"
	"encoding/json"
	"sync"
)

//go:embed data/changelog.json
var embedded []byte

// Release is one curated release note block keyed by Version (must match internal/version.Version when you want it shown).
type Release struct {
	Version string   `json:"version"`
	Title   string   `json:"title"`
	Items   []string `json:"items"`
}

type fileShape struct {
	Releases []Release `json:"releases"`
}

var (
	loadOnce sync.Once
	parsed   fileShape
	loadErr  error
)

func load() {
	loadOnce.Do(func() {
		loadErr = json.Unmarshal(embedded, &parsed)
	})
}

// ForVersion returns the release entry for the given server version, or nil if none (no modal).
func ForVersion(serverVersion string) *Release {
	load()
	if loadErr != nil || serverVersion == "" {
		return nil
	}
	for i := range parsed.Releases {
		if parsed.Releases[i].Version == serverVersion {
			r := parsed.Releases[i]
			return &r
		}
	}
	return nil
}
