package domain import ( "fmt" "strings" "time" ) // Role is a service role. Lector and helper do not exclude each other. type Role string const ( RoleLector Role = "lector" RoleHelper Role = "helper" ) var AllRoles = []Role{RoleLector, RoleHelper} func (r Role) Title() string { if r == RoleHelper { return "Kommunionhelfer" } return "Lektor" } func (r Role) Plural() string { if r == RoleHelper { return "Kommunionhelfer" } return "Lektoren" } func (r Role) ShortCode() string { if r == RoleHelper { return "K" } return "L" } func (r Role) Complementary() Role { if r == RoleLector { return RoleHelper } return RoleLector } func (r Role) IsValid() bool { return r == RoleLector || r == RoleHelper } type ChurchSymbol string const ( SymbolBasilica ChurchSymbol = "basilica" SymbolGothic ChurchSymbol = "gothic" SymbolModern ChurchSymbol = "modern" SymbolRotunda ChurchSymbol = "rotunda" SymbolRoundTower ChurchSymbol = "roundTower" SymbolChapel ChurchSymbol = "chapel" ) var AllChurchSymbols = []ChurchSymbol{ SymbolBasilica, SymbolGothic, SymbolModern, SymbolRotunda, SymbolRoundTower, SymbolChapel, } func (s ChurchSymbol) Caption() string { switch s { case SymbolBasilica: return "Romanische Basilika" case SymbolGothic: return "Neugotische Kirche" case SymbolModern: return "Moderner Kirchenbau" case SymbolRotunda: return "Rundkirche" case SymbolRoundTower: return "Rundturm" default: return "Kapelle" } } type Church struct { ID string `json:"id"` Name string `json:"name"` ShortName string `json:"shortName"` Symbol ChurchSymbol `json:"symbol"` ColorHex string `json:"colorHex"` Address string `json:"address"` SortIndex int `json:"sortIndex"` Notes string `json:"notes"` } type Person struct { ID string `json:"id"` FirstName string `json:"firstName"` LastName string `json:"lastName"` Roles []Role `json:"roles"` Email string `json:"email"` EmailLocked bool `json:"-"` PreferredChurchIDs []string `json:"preferredChurchIDs"` PreferredWeekdays []int `json:"preferredWeekdays"` PreferredServiceTitles []string `json:"preferredServiceTitles"` MaxPerMonth int `json:"maxPerMonth"` Active bool `json:"active"` Notes string `json:"notes"` } func (p Person) FullName() string { return strings.TrimSpace(p.FirstName + " " + p.LastName) } func (p Person) Initials() string { f := "" l := "" if r := []rune(p.FirstName); len(r) > 0 { f = string(r[0]) } if r := []rune(p.LastName); len(r) > 0 { l = string(r[0]) } return strings.ToUpper(f + l) } func (p Person) AbbreviatedName() string { if r := []rune(p.FirstName); len(r) > 0 { return string(r[0]) + ". " + p.LastName } return p.LastName } func (p Person) RoleLabel() string { var parts []string for _, role := range AllRoles { if p.CanFill(role) { parts = append(parts, role.Title()) } } return strings.Join(parts, " · ") } func (p Person) CanFill(role Role) bool { for _, r := range p.Roles { if r == role { return true } } return false } func (p Person) HasRole(role Role) bool { return p.CanFill(role) } func (p Person) HasPreference() bool { return len(p.PreferredChurchIDs) > 0 || len(p.PreferredWeekdays) > 0 || len(p.PreferredServiceTitles) > 0 } func IntContains(values []int, needle int) bool { for _, v := range values { if v == needle { return true } } return false } func StringContains(values []string, needle string) bool { for _, v := range values { if v == needle { return true } } return false } func UniqueStrings(values []string) []string { seen := map[string]bool{} var out []string for _, v := range values { if !seen[v] { seen[v] = true out = append(out, v) } } return out } type Absence struct { ID string `json:"id"` PersonID string `json:"personID"` Start time.Time `json:"start"` End time.Time `json:"end"` Reason string `json:"reason"` } func (a Absence) Range() DayRange { return NewDayRange(a.Start, a.End) } func (a Absence) Overlaps(t time.Time) bool { return Normalize(t).Compare(Normalize(a.Start)) >= 0 && Normalize(t).Compare(Normalize(a.End)) <= 0 } func (a Absence) Label() string { start := MediumString(a.Start) end := MediumString(a.End) base := start if start != end { base = start + " – " + end } if a.Reason == "" { return base } return base + " · " + a.Reason } type ServiceTemplate struct { ID string `json:"id"` ChurchID string `json:"churchID"` Weekday int `json:"weekday"` TimeMinutes int `json:"timeMinutes"` Title string `json:"title"` LectorsNeeded int `json:"lectorsNeeded"` HelpersNeeded int `json:"helpersNeeded"` ValidFrom time.Time `json:"validFrom"` ValidTo *time.Time `json:"validTo"` Active bool `json:"active"` Notes string `json:"notes"` } func (t ServiceTemplate) TimeLabel() string { return TimeString(t.TimeMinutes) } func (t ServiceTemplate) WeekdayLabel() string { if t.Weekday >= 1 && t.Weekday <= 7 { return weekdayNames[t.Weekday] } return "?" } func (t ServiceTemplate) RequirementLabel() string { var parts []string if t.LectorsNeeded > 0 { parts = append(parts, pluralCount(t.LectorsNeeded, "Lektor", "Lektoren")) } if t.HelpersNeeded > 0 { parts = append(parts, pluralCount(t.HelpersNeeded, "Kommunionhelfer", "Kommunionhelfer")) } if len(parts) == 0 { return "Keine Dienste" } return strings.Join(parts, " · ") } func pluralCount(n int, singular, plural string) string { if n == 1 { return "1× " + singular } return fmt.Sprintf("%d× %s", n, plural) } func (t ServiceTemplate) IsValid(on time.Time) bool { if !t.Active { return false } day := Normalize(on) if Weekday(day) != t.Weekday { return false } if day.Before(Normalize(t.ValidFrom)) { return false } if t.ValidTo != nil && day.After(Normalize(*t.ValidTo)) { return false } return true } type ServiceException struct { ID string `json:"id"` TemplateID string `json:"templateID"` Date time.Time `json:"date"` Reason string `json:"reason"` } type ServiceKind string const ( KindRegular ServiceKind = "regular" KindSpecial ServiceKind = "special" ) func (k ServiceKind) Title() string { if k == KindSpecial { return "Sondergottesdienst" } return "Regulär" } type ServiceOccurrence struct { ID string `json:"id"` ChurchID string `json:"churchID"` Location *string `json:"location"` TemplateID *string `json:"templateID"` Date time.Time `json:"date"` TimeMinutes int `json:"timeMinutes"` Title string `json:"title"` LectorsNeeded int `json:"lectorsNeeded"` HelpersNeeded int `json:"helpersNeeded"` Kind ServiceKind `json:"kind"` Notes string `json:"notes"` Locked bool `json:"locked"` CreatedAt time.Time `json:"createdAt"` } func (s ServiceOccurrence) PlaceName(church Church) string { if s.Location != nil && strings.TrimSpace(*s.Location) != "" { return *s.Location } return church.Name } func (s ServiceOccurrence) TimeLabel() string { return TimeString(s.TimeMinutes) } func (s ServiceOccurrence) DateKey() string { return Key(s.Date) } func (s ServiceOccurrence) RequiredSlots() int { return s.LectorsNeeded + s.HelpersNeeded } func (s ServiceOccurrence) RequiredCount(role Role) int { if role == RoleHelper { return s.HelpersNeeded } return s.LectorsNeeded } func (s ServiceOccurrence) RolesNeeded() []Role { var roles []Role if s.LectorsNeeded > 0 { roles = append(roles, RoleLector) } if s.HelpersNeeded > 0 { roles = append(roles, RoleHelper) } return roles } type Assignment struct { ID string `json:"id"` ServiceID string `json:"serviceID"` PersonID string `json:"personID"` Role Role `json:"role"` Locked bool `json:"locked"` Note string `json:"note"` CreatedAt time.Time `json:"createdAt"` } // Liturgical model --------------------------------------------------------- type LiturgicalRank string const ( RankWeekday LiturgicalRank = "" RankMemorial LiturgicalRank = "g" RankObligatoryMemorial LiturgicalRank = "G" RankFeast LiturgicalRank = "F" RankSolemnity LiturgicalRank = "H" ) func (r LiturgicalRank) Label() string { switch r { case RankMemorial: return "Gedenktag" case RankObligatoryMemorial: return "Gebotener Gedenktag" case RankFeast: return "Fest" case RankSolemnity: return "Hochfest" default: return "Wochentag" } } func (r LiturgicalRank) ShortLabel() string { return string(r) } func (r LiturgicalRank) Priority() int { switch r { case RankMemorial: return 1 case RankObligatoryMemorial: return 2 case RankFeast: return 3 case RankSolemnity: return 4 default: return 0 } } func (r LiturgicalRank) IsFeast() bool { return r == RankFeast || r == RankSolemnity } type LiturgicalColor string const ( ColorWhite LiturgicalColor = "w" ColorRed LiturgicalColor = "r" ColorGreen LiturgicalColor = "g" ColorViolet LiturgicalColor = "v" ColorRose LiturgicalColor = "ros" ColorBlack LiturgicalColor = "s" ColorGold LiturgicalColor = "gold" ) func (c LiturgicalColor) Label() string { switch c { case ColorRed: return "Rot" case ColorGreen: return "Grün" case ColorViolet: return "Violett" case ColorRose: return "Rosa" case ColorBlack: return "Schwarz" case ColorGold: return "Gold" default: return "Weiß" } } func LiturgicalColorFromAPI(value string) LiturgicalColor { switch strings.ToLower(strings.TrimSpace(value)) { case "r": return ColorRed case "g": return ColorGreen case "v": return ColorViolet case "ros", "rose": return ColorRose case "s", "sw": return ColorBlack case "gold": return ColorGold default: return ColorWhite } } func LiturgicalColorHex(c LiturgicalColor) string { switch c { case ColorRed: return "#A5453F" case ColorGreen: return "#4F7D5A" case ColorViolet: return "#6E4B8E" case ColorRose: return "#C07A8B" case ColorBlack: return "#4A4440" default: return "#C2A14D" } } type LiturgicalSeason string const ( SeasonAdvent LiturgicalSeason = "advent" SeasonChristmas LiturgicalSeason = "christmas" SeasonOrdinary LiturgicalSeason = "ordinary" SeasonLent LiturgicalSeason = "lent" SeasonEaster LiturgicalSeason = "easter" ) func (s LiturgicalSeason) Title() string { switch s { case SeasonAdvent: return "Advent" case SeasonChristmas: return "Weihnachtszeit" case SeasonLent: return "Österliche Bußzeit" case SeasonEaster: return "Osterzeit" default: return "Jahreskreis" } } func (s LiturgicalSeason) ShortTitle() string { switch s { case SeasonAdvent: return "Advent" case SeasonChristmas: return "Weihnachten" case SeasonLent: return "Fastenzeit" case SeasonEaster: return "Osterzeit" default: return "Jahreskreis" } } func (s LiturgicalSeason) ColorHex() string { switch s { case SeasonAdvent: return "#6E4B8E" case SeasonChristmas: return "#B58A3B" case SeasonLent: return "#75516F" case SeasonEaster: return "#C9A227" default: return "#4F7D5A" } } type FeastSymbol string const ( FeastCross FeastSymbol = "cross" FeastStar FeastSymbol = "star" FeastFlame FeastSymbol = "flame" FeastSun FeastSymbol = "sun" FeastLily FeastSymbol = "lily" FeastCrown FeastSymbol = "crown" FeastHeart FeastSymbol = "heart" FeastBook FeastSymbol = "book" FeastCandle FeastSymbol = "candle" FeastDove FeastSymbol = "dove" ) func (f FeastSymbol) Label() string { switch f { case FeastCross: return "Kreuz" case FeastStar: return "Stern" case FeastFlame: return "Feuer" case FeastSun: return "Sonne" case FeastLily: return "Lilie" case FeastCrown: return "Krone" case FeastHeart: return "Herz" case FeastBook: return "Buch" case FeastCandle: return "Licht" case FeastDove: return "Taube" default: return "" } } type Readings struct { First string `json:"first"` Psalm string `json:"psalm"` Second string `json:"second"` Gospel string `json:"gospel"` } func (r Readings) IsEmpty() bool { return r.First == "" && r.Psalm == "" && r.Second == "" && r.Gospel == "" } func (r Readings) ShortLabel() string { var parts []string if r.First != "" { parts = append(parts, r.First) } if r.Second != "" { parts = append(parts, r.Second) } if r.Gospel != "" { parts = append(parts, r.Gospel) } return strings.Join(parts, " · ") } func (r Readings) FullLabel() string { var lines []string if r.First != "" { lines = append(lines, "1. Lesung: "+r.First) } if r.Psalm != "" { lines = append(lines, "Psalm: "+r.Psalm) } if r.Second != "" { lines = append(lines, "2. Lesung: "+r.Second) } if r.Gospel != "" { lines = append(lines, "Evangelium: "+r.Gospel) } return strings.Join(lines, "\n") } type LiturgicalDay struct { Date time.Time `json:"date"` Title string `json:"title"` Rank LiturgicalRank `json:"rank"` Color LiturgicalColor `json:"color"` Remark string `json:"remark"` Readings Readings `json:"readings"` IsObligation bool `json:"isObligation"` Source string `json:"source"` UpdatedAt time.Time `json:"updatedAt"` } func (d LiturgicalDay) DateKey() string { return Key(d.Date) } func (d LiturgicalDay) IsSunday() bool { return IsSunday(d.Date) } func (d LiturgicalDay) IsHighFeast() bool { return d.Rank == RankSolemnity || strings.Contains(strings.ToLower(d.Title), "hochfest") } func (d LiturgicalDay) IsFeast() bool { return d.Rank.IsFeast() || d.IsHighFeast() } func (d LiturgicalDay) Season() LiturgicalSeason { return SeasonFor(d.Date) } func (d LiturgicalDay) Symbol() FeastSymbol { return SymbolFor(d.Title, d.Rank, d.Date) } func (d LiturgicalDay) DisplayTitle() string { if d.Title == "" { if d.IsSunday() { return "Sonntag" } return LongString(d.Date) } return d.Title } func (d LiturgicalDay) BadgeText() string { if d.IsSunday() && d.IsObligation { return "Sonntag" } if d.Rank != RankWeekday { return d.Rank.Label() } if d.IsObligation { return "Gebotener Feiertag" } return "" } func (d LiturgicalDay) ColorHex() string { return LiturgicalColorHex(d.Color) } // View support ------------------------------------------------------------- type ServiceWithAssignments struct { Service ServiceOccurrence Church Church Assignments []Assignment } func (s ServiceWithAssignments) AssignmentsFor(role Role) []Assignment { var out []Assignment for _, a := range s.Assignments { if a.Role == role { out = append(out, a) } } return out } func (s ServiceWithAssignments) OpenSlotsFor(role Role) int { open := s.Service.RequiredCount(role) - len(s.AssignmentsFor(role)) if open < 0 { return 0 } return open } func (s ServiceWithAssignments) FilledSlots() int { return len(s.Assignments) } func (s ServiceWithAssignments) OpenSlots() int { open := s.Service.RequiredSlots() - len(s.Assignments) if open < 0 { return 0 } return open } func (s ServiceWithAssignments) IsFullyStaffed() bool { return s.OpenSlots() == 0 } type DayPlanRow struct { Date time.Time LiturgicalDay *LiturgicalDay Services []ServiceWithAssignments } func (r DayPlanRow) DateKey() string { return Key(r.Date) } func (r DayPlanRow) OpenSlots() int { total := 0 for _, s := range r.Services { total += s.OpenSlots() } return total } func (r DayPlanRow) AssignmentCount() int { total := 0 for _, s := range r.Services { total += len(s.Assignments) } return total } func (r DayPlanRow) HasServices() bool { return len(r.Services) > 0 } func (r DayPlanRow) IsSunday() bool { return (r.LiturgicalDay != nil && r.LiturgicalDay.IsSunday()) || IsSunday(r.Date) } func (r DayPlanRow) IsHighFeast() bool { return r.LiturgicalDay != nil && r.LiturgicalDay.IsHighFeast() } func (r DayPlanRow) IsFeast() bool { return r.LiturgicalDay != nil && r.LiturgicalDay.IsFeast() } func (r DayPlanRow) IsSpecialDay() bool { if r.IsSunday() || r.IsFeast() { return true } return r.LiturgicalDay != nil && r.LiturgicalDay.IsObligation } func (r DayPlanRow) IsObligation() bool { return r.LiturgicalDay != nil && r.LiturgicalDay.IsObligation } func (r DayPlanRow) ServicesForChurch(churchID string) []ServiceWithAssignments { var out []ServiceWithAssignments for _, s := range r.Services { if s.Church.ID == churchID { out = append(out, s) } } return out } type UnfilledSlot struct { Service ServiceOccurrence ChurchName string Role Role Reason string } func (u UnfilledSlot) ID() string { return u.Service.ID + "-" + string(u.Role) } type PersonStat struct { Person Person Total int Lectors int Helpers int ChurchCounts map[string]int PreferredHits int SundayCount int LastAssignment *time.Time } func (s PersonStat) PreferenceRate() float64 { if s.Total == 0 { return 0 } return float64(s.PreferredHits) / float64(s.Total) } type ChurchStat struct { Church Church Assignments int Services int OpenSlots int PersonCount int } type RangeStatistics struct { Range DayRange ServiceCount int AssignmentCount int OpenSlots int RequiredSlots int PersonStats []PersonStat ChurchStats []ChurchStat MonthlyCounts map[string]int SundayServiceCount int SundayFilled int PreferenceHits int PreferenceEligible int AbsenceDays int } func (s RangeStatistics) CoverageRate() float64 { if s.RequiredSlots == 0 { return 1 } return float64(s.AssignmentCount) / float64(s.RequiredSlots) } func (s RangeStatistics) PreferenceRate() float64 { if s.PreferenceEligible == 0 { return 0 } return float64(s.PreferenceHits) / float64(s.PreferenceEligible) } func (s RangeStatistics) ActivePeople() int { n := 0 for _, p := range s.PersonStats { if p.Total > 0 { n++ } } return n } func (s RangeStatistics) AverageAssignments() float64 { count, total := 0, 0 for _, p := range s.PersonStats { if p.Total > 0 { count++ total += p.Total } } if count == 0 { return 0 } return float64(total) / float64(count) } type PlannerOptions struct { ReplaceExisting bool RespectPreferredWeekdays bool AllowDoubleRolesOnSameDay bool WeightChurchPreference float64 WeightServicePreference float64 WeightFairness float64 WeightDualRolePreference float64 MaxAssignmentsPerDay int } func DefaultPlannerOptions() PlannerOptions { return PlannerOptions{ RespectPreferredWeekdays: true, WeightChurchPreference: 8, WeightServicePreference: 4, WeightFairness: 2.2, WeightDualRolePreference: 14, MaxAssignmentsPerDay: 1, } } type PlanResult struct { Assignments []Assignment Unfilled []UnfilledSlot FilledCount int Message string } type CandidateSuggestion struct { Person Person Score float64 Warnings []string Blocked bool Preferred bool TotalAssignments int }