# API docs: https://www.silfio.com/demo

library(httr2)

# Leave api_key empty to run on the free plan (no key is sent).
api_key <- ""
base_url <- "https://silfio-production.up.railway.app"

headers <- if (nzchar(api_key)) list(`X-API-Key` = api_key) else list()

inventory <- list(
  list(
    property_name_or_address = "Warehouse A, Kyiv",
    policy_ids = list("POL-001"),
    property_type = "warehouse",
    value = 25000000,
    currency = "USD",
    latitude = 50.4501,
    longitude = 30.5234,
    iso3 = "UKR",
    property_sqm = 12000,
    building_floors = 2L,
    isic_code = "5210"
  ),
  list(
    property_name_or_address = "Office Tower, Warsaw",
    policy_ids = list("POL-002"),
    property_type = "office",
    value = 40000000,
    currency = "EUR",
    latitude = 52.2297,
    longitude = 21.0122,
    iso3 = "POL",
    property_sqm = 18500,
    building_floors = 14L,
    building_height_m = 60.0,
    isic_code = "6820"
  )
)

insurance_policies <- list(
  list(
    policy_id = "POL-001",
    policy_name = "Kyiv Warehouse Property Cover",
    insurer = "Acme Specialty",
    inception_date = "2026-01-01",
    expiry_date = "2026-12-31",
    currency = "USD",
    insurance = list(
      occurrence_attachment = 1000000,
      occurrence_limit = 20000000,
      deductible_type = "each_and_every_loss",
      participation_pct = 1.0,
      coinsurance_pct = 0.8
    )
  ),
  list(
    policy_id = "POL-002",
    policy_name = "Warsaw Office Property Cover",
    insurer = "Acme Specialty",
    inception_date = "2026-01-01",
    expiry_date = "2026-12-31",
    currency = "EUR",
    insurance = list(
      occurrence_attachment = 2000000,
      occurrence_limit = 35000000,
      deductible_type = "each_and_every_loss",
      participation_pct = 0.5
    )
  )
)

investments <- list(
  list(
    asset_class = "Bond",
    instrument_type = "government_bond",
    issuer = "Republic of Poland",
    currency = "EUR",
    market_value = 5000000,
    iso3 = "POL",
    credit_rating = "A-",
    duration = 6.5
  ),
  list(
    asset_class = "Equity",
    instrument_type = "listed_equity",
    issuer = "Baltic Energy PLC",
    currency = "USD",
    market_value = 3000000,
    iso3 = "LTU",
    industry = "utilities"
  ),
  list(
    asset_class = "Cash",
    instrument_type = "deposit",
    issuer = "Regional Bank",
    currency = "USD",
    market_value = 1500000,
    iso3 = "UKR",
    duration = 0.1
  )
)

event_scenario <- list(
  UKR = list(annual_prob = 1.0, conflict_type = "interstate")
)

hazard_scenario <- list(
  UKR = list(damage = 0.30, gdp_loss = -0.15, equity_loss = -0.25),
  POL = list(damage = 0.05, equity_loss = -0.08)
)

payload <- list(
  inventory = inventory,
  insurance_policies = insurance_policies,
  investments = investments
)

scenario_payload <- c(payload, list(
  event_scenario = event_scenario,
  hazard_scenario = hazard_scenario
))

run <- request(paste0(base_url, "/run")) |>
  req_headers(!!!headers) |>
  req_body_json(payload, auto_unbox = TRUE) |>
  req_timeout(300) |>
  req_perform()
writeBin(resp_body_raw(run), "silfio_run.zip")

# Scenario testing is an enterprise-only feature. Always send the request; the
# API returns 403 if the key (or session) isn't authorised for it.
scenario <- request(paste0(base_url, "/scenario")) |>
  req_headers(!!!headers) |>
  req_body_json(scenario_payload, auto_unbox = TRUE) |>
  req_timeout(300) |>
  req_error(is_error = function(resp) FALSE) |>
  req_perform()
if (resp_status(scenario) < 400) {
  writeBin(resp_body_raw(scenario), "silfio_scenario.zip")
} else {
  message(sprintf(
    "Scenario request rejected (%d): %s",
    resp_status(scenario), resp_body_string(scenario)
  ))
}
