#import "data/config.typ": *
#import "data/theme.typ": *
#import "data/cover.typ": cover-page
#import "data/toc.typ": toc-page, toc-register-chapter-counter
#import "data/metropole-grelin.typ": metropole, start-body-page-numbering
#import "data/glossary.typ": init-glossary, register-report-glossary, print-glossary, glossary-entries, gls-long, gls-short
#import "data/diagrams.typ": ore-sorting-flow
#import "data/code.typ": codly-init-rule, codly-config, code-token-highlight-init
#import "data/md.typ": load-report-md

#show: codly-init-rule
#show: code-token-highlight-init
#show: init-glossary
#codly-config

#register-report-glossary()

#let report = load-report-md(
  read(content-path),
  source-path: content-path,
  extra-scope: (
    ore-sorting-flow: ore-sorting-flow,
    gls-long: gls-long,
    gls-short: gls-short,
  ),
)
#let meta = report.metadata

#let parse-report-date(raw) = {
  if type(raw) == datetime {
    raw
  } else if type(raw) == str {
    let parts = raw.split("-")
    if parts.len() == 3 {
      datetime(
        year: int(parts.at(0)),
        month: int(parts.at(1)),
        day: int(parts.at(2)),
      )
    } else {
      none
    }
  } else {
    none
  }
}

#let report-date = parse-report-date(meta.date)

#cover-page(meta, date: report-date)

#show: metropole.with(
  title: meta.title,
  subtitle: none,
  author: meta.brand,
  date: report-date,
  language: "zh",
  paper-size: "a4",
  cover-page: false,
  inline-title: false,
  accent-color: color-primary,
  body-font: body-font,
  heading-font: heading-font,
  raw-font: mono-font,
  font-size: size-body,
  leading-ratio: leading-ratio,
  margin-x: page-margin-x,
  margin-y: page-margin-y,
)

#set heading(numbering: "1.1.1")
#show: toc-register-chapter-counter()

#toc-page(meta)

#if meta.summary != "" [
  #v(summary-top-offset, weak: false)
  #text(font: heading-font, size: size-h1, weight: "bold", fill: color-heading)[摘要]
  #v(tw(3))
  #meta.summary
  #v(summary-body-gap)
]

#start-body-page-numbering()

#report.body

#pagebreak(weak: true)
#heading(outlined: false, level: 1)[术语与缩写]
#print-glossary(glossary-entries, show-all: true)
