---
# The title is intentionally commented out.
#
# In Quarto's Typst format, a YAML title is rendered as a visible title block.
# This CV builds its own custom header below, so leaving `title` active would
# print an unwanted heading before the profile image and header block.
# title: "Your Name CV"

%%CURRICULR_FORMAT%%

params:
  use_icons: "__CURRICULR_USE_ICONS__"
  variant: "__CURRICULR_VARIANT__"

execute:
  echo: false
  warning: false
  message: false
---

```{r}
#| label: setup

library(curriculr)

# Path to the Excel workbook and variant are injected by create_cv() at render
# time. Do not edit these lines manually — run create_cv() again to regenerate.
cv <- read_cv_data("__CURRICULR_DATA_PATH__", variant = params$variant)
```

%%CURRICULR_THEME%%

```{r}
#| label: header
#| results: asis

# All personal information comes from the profile sheet of the workbook.
# Edit the workbook, not this file, to update your personal information.

first_name        <- cv$profile[["first_name"]]
last_name         <- cv$profile[["last_name"]]
address           <- cv$profile[["address"]]
profile_statement <- cv$profile[["profile_statement"]]

# Photo path injected by create_cv() at render time.
# An empty string means no photo was supplied — single-column layout is used.
# Do not edit this line manually — run create_cv() again to regenerate.
photo <- "__CURRICULR_PHOTO_PATH__"

# Escape profile values for safe use in Typst
first_name        <- typst_escape(first_name)
last_name         <- typst_escape(last_name)
address           <- typst_escape(address)
profile_statement <- typst_escape(profile_statement)

# Build the contact line. cv_contact_line() handles icon lookup, plain text
# fallback, and separator style based on use_icons.
contact_line <- cv_contact_line(cv$profile, use_icons = params$use_icons)

# Emit the header block. When a photo path is present, use a two-column grid
# with the image on the left. When photo is empty, use a single full-width
# column so the layout doesn't leave a blank gap on the left.
if (nzchar(photo)) {
  cat(sprintf(
    paste0(
      '\n```{=typst}\n',
      '#grid(columns: (1.36in, 1fr), gutter: 0.42in,\n',
      '  [#image("%s", width: 1.27in)],\n',
      '  [\n',
      '    #align(center)[\n',
      '      #text(size: 27pt, weight: "light",  fill: rgb("#777777"))[%s]',
      '#h(0.16em)',
      '#text(size: 27pt, weight: "bold", fill: dark)[%s]\n',
      '      #v(0.16em)\n',
      '      #text(size: 8.2pt, fill: lightgray)[%s]\n',
      '      #v(0.18em)\n',
      '      #text(size: 8.3pt, fill: lightgray)[%s]\n',
      '      #v(0.46em)\n',
      '      #text(size: 9pt, style: "italic", fill: bodygray)[%s]\n',
      '    ]\n',
      '  ]\n',
      ')\n',
      '#v(0.85em)\n',
      '```\n'
    ),
    photo, first_name, last_name,
    address, contact_line, profile_statement
  ))
} else {
  cat(sprintf(
    paste0(
      '\n```{=typst}\n',
      '#align(center)[\n',
      '  #text(size: 27pt, weight: "light",  fill: rgb("#777777"))[%s]',
      '#h(0.16em)',
      '#text(size: 27pt, weight: "bold", fill: dark)[%s]\n',
      '  #v(0.16em)\n',
      '  #text(size: 8.2pt, fill: lightgray)[%s]\n',
      '  #v(0.18em)\n',
      '  #text(size: 8.3pt, fill: lightgray)[%s]\n',
      '  #v(0.46em)\n',
      '  #text(size: 9pt, style: "italic", fill: bodygray)[%s]\n',
      ']\n',
      '#v(0.85em)\n',
      '```\n'
    ),
    first_name, last_name,
    address, contact_line, profile_statement
  ))
}
```

```{r}
#| label: cv-sections
#| results: asis

# Section rendering is driven entirely by the sections sheet in the workbook.
#
# Each row in cv$sections produces one CV section in the order the rows appear.
# To exclude a section, delete its row from the sections sheet.
# To reorder sections, reorder the rows in the sections sheet.
# To add a new section, add a row — the section name must match a sheet in
# the workbook and that sheet must follow the standard column schema.

for (i in seq_len(nrow(cv$sections))) {
  sec <- cv$sections[i, ]

  section_name <- trimws(as.character(sec[["section"]]))
  label        <- trimws(as.character(sec[["label"]]))
  title_col    <- trimws(as.character(sec[["title_col"]] %||% "title"))
  org_col      <- trimws(as.character(sec[["org_col"]]   %||% ""))
  detail_col   <- trimws(as.character(sec[["detail_col"]]%||% ""))
  where_col    <- trimws(as.character(sec[["where_col"]] %||% ""))
  date_token   <- trimws(as.character(sec[["date_fun"]]  %||% "none"))

  # Resolve empty strings to NULL for optional arguments
  org_col    <- if (nzchar(org_col))    org_col    else NULL
  detail_col <- if (nzchar(detail_col)) detail_col else NULL
  where_col  <- if (nzchar(where_col))  where_col  else NULL

  # Resolve the date_fun token to an R function (or NULL)
  date_fun <- resolve_date_fun(date_token)

  # Retrieve the section data frame from the cv list
  section_data <- cv[[section_name]]

  # Skip gracefully if the sheet is missing or empty
  if (is.null(section_data) || nrow(section_data) == 0) {
    cli::cli_alert_warning(
      "Section {.val {section_name}} is listed in the sections sheet but
       has no data. Skipping."
    )
    next
  }

  # Emit the section heading and entries
  cat(cv_section(label))
  cv_render_section(
    data       = section_data,
    title_col  = title_col,
    org_col    = org_col,
    detail_col = detail_col,
    date_fun   = date_fun,
    where_col  = where_col
  )
}
```
