using Elternbeirat.Contracts;
namespace Elternbeirat.Web.Shared;
///
/// Formats the date range of an for display in German.
///
///
/// Shared by the full event list and the home-page events embed, so both render
/// dates the same way.
///
public static class EventFormat
{
///
/// Date format for the start, and for a single-day event.
///
private const string LongDate = "dddd, d. MMMM yyyy";
///
/// Date format for the end of a multi-day event; the weekday is omitted.
///
private const string ShortDate = "d. MMMM yyyy";
///
/// Formats the date range of an event.
///
///
/// The event to format.
///
///
/// The formatted range, depending on and :
///
/// -
/// No end: Donnerstag, 8. Oktober 2026, 19:30 Uhr
///
/// -
/// Same day: Donnerstag, 8. Oktober 2026, 19:30–21:00 Uhr
///
/// -
/// Several days: Freitag, 9. Oktober 2026 – 11. Oktober 2026
///
///
/// Without a time, the time parts are omitted.
///
public static string Range(Event @event)
=> (@event.Start, @event.End, @event.HasTime) switch
{
(var start, null, var hasTime)
=> Stamp(start, LongDate, hasTime),
(var start, { } end, true) when end.Date == start.Date
=> $"{start.ToString(LongDate, Cultures.German)}, {start.ToString("HH:mm", Cultures.German)}–{end.ToString("HH:mm", Cultures.German)} Uhr",
(var start, { } end, false) when end.Date == start.Date
=> Stamp(start, LongDate, withTime: false),
(var start, { } end, var hasTime)
=> $"{Stamp(start, LongDate, hasTime)} – {Stamp(end, ShortDate, hasTime)}",
};
///
/// Formats a single point in time as a date with an optional time.
///
///
/// The date and time to format.
///
///
/// The date format, e.g. .
///
///
/// to append the time, e.g. , 19:30 Uhr.
///
///
/// The formatted date, with the time if requested.
///
private static string Stamp(DateTime value, string dateFormat, bool withTime)
=> withTime
? value.ToString(dateFormat + ", HH:mm", Cultures.German) + " Uhr"
: value.ToString(dateFormat, Cultures.German);
}