package components

import (
	"dk/internal/helpers/markdown"
	"dk/internal/models/news"
	"dk/internal/models/users"
	"fmt"
	"time"
)

func GenerateTownNews() string {
	title := `<div class="title">Latest News</div>`

	news, err := news.Recent(1)
	if err == nil && len(news) > 0 {
		item := news[0]
		content := fmt.Sprintf(`<span class="light">%s</span>`, item.ReadableTime())
		return title + content + fmt.Sprintf(`<div>%s</div>`, markdown.MarkdownToHTML(item.Content))
	}

	return title + "<div>No news!</div>"
}

func GenerateTownWhosOnline() string {
	title := `<div class="title">Who's Online</div>`

	onlineUsers, err := users.Online(10 * time.Minute)
	if err == nil && len(onlineUsers) > 0 {
		if len(onlineUsers) == 1 {
			title += "<p>There is 1 user online in the last 10 minutes:</p>"
		} else {
			title += fmt.Sprintf("<p>There are %d users online in the last 10 minutes:</p>", len(onlineUsers))
		}

		for _, user := range onlineUsers {
			title += fmt.Sprintf(`<div>%s</div>`, user.Username)
		}
		return title
	}

	return title + "<div>No one!</div>"
}
