package handlers

import (
	"net/http"

	"mgit.msbls.de/m/paliad/internal/changelog"
)

// handleChangelogPage serves the static /changelog HTML shell. The entries
// are fetched client-side via /api/changelog.
func handleChangelogPage(w http.ResponseWriter, r *http.Request) {
	http.ServeFile(w, r, "dist/changelog.html")
}

// handleChangelogAPI returns every entry, newest first.
func handleChangelogAPI(w http.ResponseWriter, r *http.Request) {
	writeJSON(w, http.StatusOK, changelog.All())
}

// handleChangelogUnseenCount returns how many entries are newer than the
// ?since=<ISO timestamp> query parameter. Missing / empty since is treated
// as "never seen" so the badge shows on a user's very first session.
//
// ISO 8601 timestamps compare correctly as strings against YYYY-MM-DD
// entry dates (a timestamp for 2026-04-22T10:00:00Z sorts after the date
// "2026-04-22", which is what we want — same-day entries posted at 00:00
// are considered seen by mid-day callers).
func handleChangelogUnseenCount(w http.ResponseWriter, r *http.Request) {
	since := r.URL.Query().Get("since")
	writeJSON(w, http.StatusOK, map[string]int{"count": changelog.UnseenCount(since)})
}
