## ----setup, include = FALSE---------------------------------------------------
fixture_dir <- "media-generation"
recording <- nzchar(Sys.getenv("FOUNDRY_RECORD_DOCS"))
have_fixtures <- dir.exists(fixture_dir) && length(list.files(fixture_dir)) > 0
run_api <- requireNamespace("httptest2", quietly = TRUE) &&
  (recording || have_fixtures)
library(foundryR)
if (run_api) {
  httptest2::start_vignette(fixture_dir)
}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", eval = run_api,
  fig.width = 7, fig.height = 4.5, out.width = "100%")

embed_image <- function(path, width = "60%", alt = "Generated image") {
  if (!requireNamespace("base64enc", quietly = TRUE)) {
    return(invisible(NULL))
  }
  uri <- paste0("data:image/jpeg;base64,", base64enc::base64encode(path))
  knitr::asis_output(
    sprintf('<img src="%s" width="%s" alt="%s">', uri, width, alt)
  )
}

## ----library, eval = TRUE, message = FALSE------------------------------------
library(foundryR)

## ----image-config, eval = FALSE-----------------------------------------------
# foundry_set_image_endpoint(Sys.getenv("AZURE_FOUNDRY_IMAGE_ENDPOINT"))
# foundry_set_image_key("your-image-api-key")
# 
# Sys.setenv(AZURE_FOUNDRY_IMAGE_MODEL = "my-image-deployment")

## ----generate-----------------------------------------------------------------
image <- foundry_image(
  "A simple flat vector illustration of an empty classroom with desks, a chalkboard, and soft daylight, no people, no logos, no text",
  model = "gpt-image-2",
  size = "1024x1024",
  quality = "low",
  output_format = "jpeg",
  output_compression = 40
)

image[, c("prompt", "output_format", "created")]

## ----save-image---------------------------------------------------------------
img_path <- tempfile(fileext = ".jpeg")
suppressMessages(invisible(foundry_save_image(image, img_path)))
file.exists(img_path)

## ----display-image, echo = FALSE----------------------------------------------
embed_image(
  img_path,
  alt = "AI-generated flat vector illustration of an empty classroom with desks and a chalkboard"
)

## ----edit-call----------------------------------------------------------------
edited <- foundry_image_edit(
  image = img_path,
  prompt = paste(
    "Add stacks of papers and cardboard boxes on the desks.",
    "Keep the room, desks, chalkboard, light, and illustration style unchanged."
  ),
  model = "gpt-image-2",
  quality = "low",
  output_format = "jpeg"
)

edited_path <- tempfile(fileext = ".jpeg")
suppressMessages(invisible(foundry_save_image(edited, edited_path)))

## ----display-pair, echo = FALSE-----------------------------------------------
knitr::asis_output(paste(
  embed_image(
    img_path,
    width = "48%",
    alt = "Original stimulus: an empty, tidy classroom illustration"
  ),
  embed_image(
    edited_path,
    width = "48%",
    alt = "Edited variant: the same classroom with papers and boxes stacked on the desks"
  )
))

## ----cleanup, include = FALSE, eval = TRUE------------------------------------
if (run_api) {
  unlink(c(img_path, edited_path))
  httptest2::end_vignette()
}

