-- stress_mixed_endpoints.lua
-- 50% /api/v2/pagedata
-- 25% /api/v2/pagecontent
-- 12.5% /api/v1/visual
-- 12.5% /api/v1/micro-markup

-- ===== Tunables (can be overridden via env) =====
local i_min   = tonumber(os.getenv("I_MIN") or "1")
local i_max   = tonumber(os.getenv("I_MAX") or "2500000")

local language    = os.getenv("LANGUAGE") or "en"
local domain      = os.getenv("DOMAIN") or "advcache.example.com"
local project_id  = os.getenv("PROJECT_ID") or "285"

-- Weights for routing (defaults: 0.50 / 0.25 / 0.125 / 0.125)
local w_pagedata      = tonumber(os.getenv("W_PAGEDATA") or "0.5")
local w_pagecontent   = tonumber(os.getenv("W_PAGECONTENT") or "0.25")
local w_visual        = tonumber(os.getenv("W_VISUAL") or "0.125")
local w_micro_markup  = tonumber(os.getenv("W_MICRO_MARKUP") or "0.125")

-- Common headers
local headers = {
  ["Host"]             = os.getenv("HOST") or "0.0.0.0:8020",
  ["Accept-Encoding"]  = os.getenv("AE") or "gzip, deflate, br",
  ["Accept-Language"]  = os.getenv("ALANG") or "en-US,en;q=0.9"
}

-- ===== Per-thread seeding to avoid correlated RNG across threads =====
local thread_id = 0
function setup(thread)
  thread:set("tid", thread_id)
  thread_id = thread_id + 1
end

function init(args)
  local tid = tid or 0
  math.randomseed(os.time() + tid * 10007)
  for _ = 1, 5 do math.random() end
end

-- ===== Helpers =====
local function build_query(i)
  return
    "?project[id]=" .. project_id ..
    "&domain=" .. domain ..
    "&language=" .. language ..
    "&choice[name]=betting" ..
    "&choice[choice][name]=betting_live" ..
    "&choice[choice][choice][name]=betting_live_null" ..
    "&choice[choice][choice][choice][name]=betting_live_null_" .. i ..
    "&choice[choice][choice][choice][choice][name]=betting_live_null_" .. i .. "_" .. i ..
    "&choice[choice][choice][choice][choice][choice][name]=betting_live_null_" .. i .. "_" .. i .. "_" .. i ..
    "&choice[choice][choice][choice][choice][choice][choice]=null"
end

-- precalc cumulative cutoffs to avoid float drift
local function normalize_weights()
  local sum = w_pagedata + w_pagecontent + w_visual + w_micro_markup
  if sum <= 0 then
    -- fall back to defaults if someone passes zeros
    w_pagedata, w_pagecontent, w_visual, w_micro_markup = 0.5, 0.25, 0.125, 0.125
    sum = 1.0
  end
  w_pagedata     = w_pagedata     / sum
  w_pagecontent  = w_pagecontent  / sum
  w_visual       = w_visual       / sum
  w_micro_markup = w_micro_markup / sum

  -- cumulative
  return {
    w_pagedata,
    w_pagedata + w_pagecontent,
    w_pagedata + w_pagecontent + w_visual,
    1.0 -- implicit final
  }
end

local cut = normalize_weights()

-- ===== Request generator =====
request = function()
  local i = math.random(i_min, i_max)
  local q = build_query(i)

  local r = math.random()
  local path
  if r < cut[1] then
    path = "/api/v2/pagedata" .. q
  elseif r < cut[2] then
    path = "/api/v2/pagecontent" .. q
  elseif r < cut[3] then
    path = "/api/v1/visual" .. q
  else
    path = "/api/v1/micro-markup" .. q
  end

  return wrk.format("GET", path, headers)
end
