// DateTime guide
// A datetime declared without an initializer uses the current system date and time.
// Use format(...) to render it with Def masks such as hh:mm:ss, dd/mm/yy, or dd/mm/yyyy.
// This example also shows functions, if, match, print, and assertions.

def now as datetime
def another_now as datetime()
def configured as datetime

def only_time as string(now.format("hh:mm:ss"))
def full_date as string(now.format("dd/mm/yyyy"))
def literal_value as string()

configured.day(1)
configured.month(1)
configured.year(2026)
configured.hour(8)
configured.minute(30)
configured.second(45)

def render_literal as function(value as datetime) (
  value.format("literal")
)

def label as string(
  match render_literal(now) (
    "literal" => "formatted",
    _ => "unexpected"
  )
)

literal_value = render_literal(now)

if literal_value == "literal" (
  print("datetime if branch")
) else (
  print("datetime else branch")
)

print(now)
print(another_now.format("hh:mm:ss dd/mm/yy"))
print(only_time)
print(full_date)
print(configured.format("hh:mm:ss dd/mm/yyyy"))
print(configured.day())
print(configured.month())
print(configured.year())
print(configured.hour())
print(configured.minute())
print(configured.second())
print(label)

assert(literal_value == "literal")
assert(configured.day() == 1)
assert(configured.month() == 1)
assert(configured.year() == 2026)
assert(configured.hour() == 8)
assert(configured.minute() == 30)
assert(configured.second() == 45)
assert(label == "formatted")
print("all assertions passed")

// Const — immutable datetime binding; read-only field access still works
def epoch as const datetime
assert(epoch.format("hh:mm:ss") != "")
// epoch.day(15)  // would fail: cannot call 'day()' on const datetime
print("all assertions passed")

// Command-line parameter support
// Run with: def run datetime.def --param report_date=2026-01-01
// or with full RFC3339: --param report_date=2026-01-01T00:00:00+00:00
def report_date as datetime(from_cmd_param("report_date", now))
def report_date_str as string(report_date.format("dd/mm/yyyy"))
print("report_date (--param or default): {{report_date_str}}")
