package core

import "time"

// ParseDateOrDefault parses s as a date/datetime and returns the result.
// Accepted formats: "2006-01-02" (YYYY-MM-DD) and time.RFC3339Nano.
// Returns dflt when s is empty or does not match either format.
// When endOfDay is true and the input matched YYYY-MM-DD, adds
// 24*time.Hour - time.Nanosecond so the result is the last nanosecond of
// that day (useful for inclusive end-of-range queries).
// RFC3339Nano inputs are never adjusted regardless of endOfDay.
func ParseDateOrDefault(s string, dflt time.Time, endOfDay bool) time.Time {
	if s == "" {
		return dflt
	}
	if t, err := time.Parse("2006-01-02", s); err == nil {
		if endOfDay {
			return t.Add(24*time.Hour - time.Nanosecond)
		}
		return t
	}
	if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
		return t
	}
	return dflt
}
