Module:StraitsDaily: Difference between revisions

From MicrasWiki
Jump to navigationJump to search
NewZimiaGov (talk | contribs)
No edit summary
NewZimiaGov (talk | contribs)
No edit summary
Line 1: Line 1:
-- Module: StraitsDaily
-- Module: StraitsDaily
-- Purpose: Daily, in-world report of Strait of Haifa enforcement [[Straits Conventions of 52.06 PSSC]]
-- Purpose: Daily, in-world report of Strait of Haifa enforcement [[Straits Conventions of 52.06 PSSC]]


local p = {}
local p = {}
Line 130: Line 131:
     ["Independent Coastal State"] = true
     ["Independent Coastal State"] = true
}
}
-------------------------------------------------------------------------------
-- (B2) PUBLIC HEALTH: DISEASES & CONTROLS (same canon as outbreaks modules)
-------------------------------------------------------------------------------
local phDiseases = {
  {name="Thalassan Rot",        code="TR", controls="canal flush; rat-proofing; food-handler checks"},
  {name="Festival Blush",      code="FB", controls="ring vaccination; crowd spacing; dorm rotation"},
  {name="Pilgrim’s Flux",      code="PF", controls="boil notices; cistern audit; ORS distribution"},
  {name="Stygian Dust",        code="SD", controls="respirators; mist carts; guide clinic checks"},
  {name="Haifan Salt-Sickness", code="HS", controls="indoor rotations; antifungal clinics; dehumidify"},
  {name="Norsolyrian Drain",    code="ND", controls="lined ditches; praziquantel; snail abatement"},
  {name="Somniumpolis Lung",    code="SL", controls="dust suppression; respirator fit-checks"},
  {name="Haifan Crimson Fever", code="HCF",controls="larval control; dusk patrols; vector abatement"},
  {name="Strip Yellowing",      code="SY", controls="single-use ladles; vendor relicensing; recall"},
  {name="Eye of Styx",          code="ES", controls="eye-wash stations; visor compliance"},
  {name="Cato’s Curse",        code="CC", controls="PPE audits; solvent/oxidizer exposure controls"},
  {name="White Pestilence",    code="WP", controls="tiered quarantine; convoy assays; cordons"},
  {name="Breath of Nephele",    code="BN", controls="ventilation drills; mask catechism; spacing"},
  {name="Agnian Lockjaw",      code="AL", controls="tetanus boosters; injury clinics"},
}
local phIndex = {}
for _,d in ipairs(phDiseases) do phIndex[d.code] = d end
-- Decide if a row merits a PH advisory; return code and text or nil
local function healthConcern(sector, flag, vessel, cargo, tag, partner, riskPush, rnd, baseRate)
    -- baseRate is a small background chance when triggers are weak (default 0.35)
    local score = 0
    -- Sector effects (coastal, sacred, environmental buffers)
    if sector == "Environmental Buffer" or sector == "Sacred Buffer Approaches"
      or sector:find("Strait") or sector:find("Approach") then
        score = score + 1
    end
    -- Cargo/tag effects
    local cname = cargo and cargo.name or ""
    local tname = tag and tag.name or "standard"
    if cname == "Agricultural commodities" or tname == "perishable" then score = score + 2 end
    if cname == "Relief supplies (mixed pallets)" then score = score + 1 end
    if cname == "Aviation fuels & additives" or cname == "Refined petroleum products"
      or cname == "Hydraulic lubricants (dual-use)" or cname:find("oxidizers") then score = score + 2 end
    -- Crowding signals (ferries, expedited/emergency)
    if vessel == "Ro-Ro ferry" and (tname == "expedited" or tname == "emergency") then score = score + 2 end
    -- Partner/flag risk
    if (partner and partner.risk or 0) >= 0.09 then score = score + 1 end
    if flag and flag.risky then score = score + 0.5 end
    -- Enforcement multi-trigger
    score = score + (riskPush or 0) * 0.5
    -- Choose disease based on dominant trigger theme
    local function pick(code)
        local d = phIndex[code]
        if not d then return nil end
        return d.code, (d.code .. " ("..d.name.."): " .. d.controls)
    end
    -- Rare escalation: White Pestilence screening on very high scores
    if score >= 5 and rnd() < 0.10 then
        return pick("WP")
    end
    -- Food/water hygiene path
    if (cname == "Agricultural commodities" or tname == "perishable") and rnd() < 0.75 then
        return pick("SY")
    end
    if (cname == "Relief supplies (mixed pallets)" or score >= 3) and rnd() < 0.40 then
        return pick("PF")
    end
    -- Waterborne/coastal path
    if (sector == "Environmental Buffer" or sector:find("Strait")) and rnd() < 0.50 then
        return pick("TR")
    end
    if sector == "Archipelago Zone" and rnd() < 0.30 then
        return pick("HS")
    end
    -- Solvent/oxidizer/industrial exposure
    if (cname:find("oxidizers") or cname:find("lubricants") or cname:find("petroleum") or cname:find("fuels")) and rnd() < 0.70 then
        return pick("CC")
    end
    -- Crowd respiratory
    if vessel == "Ro-Ro ferry" and (tname == "expedited" or tname == "emergency") and rnd() < 0.60 then
        return pick("FB")
    end
    if partner and partner.risk >= 0.09 and rnd() < 0.30 then
        return pick("BN")
    end
    -- Low background chance
    if rnd() < (baseRate or 0.35) then
        -- pick a mild, generic hygiene signal weighted toward SY/PF
        local r = rnd()
        if r < 0.45 then return pick("SY")
        elseif r < 0.70 then return pick("PF")
        elseif r < 0.85 then return pick("TR")
        else return pick("BN") end
    end
    return nil
end


-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- (C) POLICY STAGES (per-flag mapping)
-- (C) POLICY STAGES (per-flag mapping)
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Default global stage remains available via |policy_stage=..., but these
-- per-flag settings override it when present.
local stageMap = {
local stageMap = {
     ["Imperial Federation"] = 1,  -- Stage I (audit emphasis)
     ["Imperial Federation"] = 1,  -- Stage I (audit emphasis)
     ["Matamoros (IF)"]      = 3,  -- Stage III (limited denial on high-risk, non-WL rows)
     ["Matamoros (IF)"]      = 3,  -- Stage III (limited denial on high-risk, non-WL rows)
}
}
local function getFlagStage(flagName, globalStage)
local function getFlagStage(flagName, globalStage)
     local s = stageMap[flagName]
     local s = stageMap[flagName]
Line 151: Line 249:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
local function pick(list) return list[math.random(#list)] end
local function pick(list) return list[math.random(#list)] end
local function pickWeighted(list)
local function pickWeighted(list)
     local total = 0
     local total = 0
Line 185: Line 282:
end
end


-- Legal basis per Conventions
local function legalBasis(sector, wl, wl03note)
local function legalBasis(sector, wl, wl03note)
     if wl then
     if wl then
Line 196: Line 292:
end
end


-- Appeals column
local function appealsStatus(sector, wl, tag, cargo)
local function appealsStatus(sector, wl, tag, cargo)
     if wl then
     if wl then
Line 217: Line 312:
end
end


-- HCE reference for WL notes
local function hceRef(dateInfo, corridor)
local function hceRef(dateInfo, corridor)
     return string.format("HCE-%03d.%03d-%s", dateInfo.year, dateInfo.dayOfYear, corridor.code)
     return string.format("HCE-%03d.%03d-%s", dateInfo.year, dateInfo.dayOfYear, corridor.code)
end
end


-- Pick a served district for a corridor and annotate with kind
local function pickServedDistrict(corr)
local function pickServedDistrict(corr)
     local dname = corr.districts[math.random(#corr.districts)]
     local dname = corr.districts[math.random(#corr.districts)]
Line 229: Line 322:
end
end


-- Row color based on outcome/notes
local function rowColor(outcome, notes, wlEligible)
    local o = outcome:lower()
    local n = (notes or ""):lower()
    if o:match("hold") or o:match("denial") or o:match("detention") or o:match("impound") or n:match("extended regulatory review") then
        return ' style="background-color:#f8d7da;"' -- red
    end
    if o:match("administrative notice") or o:match("monitoring") or o:match("conditions") or o:match("reconciliation")
      or n:match("wl%-ineligible") or n:match("risk tag: m%-amber") or n:match("surcharge advisory") then
        return ' style="background-color:#fff3cd;"' -- yellow
    end
    return ' style="background-color:#d4edda;"' -- green
end
-- Build a human-readable list of triggers for transparency column
local function buildTriggers(flag, sector, cargo, tag, partner, wlEligible, wl03note, riskPush, flagStage)
local function buildTriggers(flag, sector, cargo, tag, partner, wlEligible, wl03note, riskPush, flagStage)
     local t = {}
     local t = {}
Line 270: Line 348:
     math.randomseed(dateInfo.year*1000 + dateInfo.dayOfYear)
     math.randomseed(dateInfo.year*1000 + dateInfo.dayOfYear)


     local rows = tonumber(frame and frame.args and frame.args.rows) or 18
     local rows       = tonumber(frame and frame.args and frame.args.rows) or 18
     local globalStage = tonumber(frame and frame.args and frame.args.policy_stage) or 0
     local globalStage = tonumber(frame and frame.args and frame.args.policy_stage) or 0
     local showLegend = (frame and frame.args and tostring(frame.args.showlegend) or "yes"):lower() ~= "no"
     local showLegend = (frame and frame.args and tostring(frame.args.showlegend) or "yes"):lower() ~= "no"
 
    -- Public health toggles
    local phOn        = (frame and frame.args and tostring(frame.args.ph) or "on"):lower() ~= "off"
    local phRate      = tonumber(frame and frame.args and frame.args.phrate) or 0.35
    local showPHLeg  = (frame and frame.args and tostring(frame.args.showphlegend) or "yes"):lower() ~= "no"


     local out = {}
     local out = {}
     table.insert(out, "{| class='wikitable sortable'")
     table.insert(out, "{| class='wikitable sortable'")
     table.insert(out, "! Date !! Sector !! Vessel/Flag !! Action !! Legal Basis !! Outcome !! Appeals/Review (Straits Conventions) !! Case ID !! Triggers !! Notes")
     if phOn then
        table.insert(out, "! Date !! Sector !! Vessel/Flag !! Action !! Legal Basis !! Outcome !! Appeals/Review (Straits Conventions) !! Case ID !! PH Case !! Public Health Advisory !! Triggers !! Notes")
    else
        table.insert(out, "! Date !! Sector !! Vessel/Flag !! Action !! Legal Basis !! Outcome !! Appeals/Review (Straits Conventions) !! Case ID !! Triggers !! Notes")
    end


     for i = 1, rows do
     for i = 1, rows do
Line 309: Line 396:
             local isMT = flag.name:match("^Matamoros")
             local isMT = flag.name:match("^Matamoros")
             if (isIF or isMT) and not wlEligible then
             if (isIF or isMT) and not wlEligible then
                 if flagStage >= 1 then q = q + 0.04 end     -- Stage I: audit emphasis
                 if flagStage >= 1 then q = q + 0.04 end
                 if flagStage >= 2 then q = q + 0.04 end     -- Stage II: surcharge advisory (total +0.08)
                 if flagStage >= 2 then q = q + 0.04 end
             end
             end


             -- M-AMBER effect for WL-03 unsigned (Matamoros default false)
             -- M-AMBER effect
             if flag.name:match("Matamoros") and wl03note == "WL-03 not signed (M-AMBER)" then
             if flag.name:match("Matamoros") and wl03note == "WL-03 not signed (M-AMBER)" then
                 q = q + 0.08
                 q = q + 0.08
             end
             end


             -- Bassaridian-flag very high compliance
             -- Bassaridian-flag compliance
             if flag.name == "Bassaridian-flag" then
             if flag.name == "Bassaridian-flag" then
                 q = q - 0.18
                 q = q - 0.18
Line 380: Line 467:
             end
             end


             -- Stage III: limited denial logic for IF/Matamoros non-WL rows at high risk
             -- Stage III denial logic
             local isIF = flag.name == "Imperial Federation"
             local isIF = flag.name == "Imperial Federation"
             local isMT = flag.name:match("^Matamoros")
             local isMT = flag.name:match("^Matamoros")
Line 395: Line 482:


             appealCol = appealCol or appealsStatus(sector, wlEligible, tag, cargo)
             appealCol = appealCol or appealsStatus(sector, wlEligible, tag, cargo)
        end
        -- Public Health advisory (deterministic per row)
        local phCaseId, phText = "", ""
        if phOn then
            -- stabilize PH random draws per-row by reseeding on top of the daily seed
            local rowSeed = (dateInfo.year*1000 + dateInfo.dayOfYear) + i*131
            math.randomseed(rowSeed)
            local function rnd() return math.random() end
            local code, text = healthConcern(sector, flag, vessel, cargo, tag, partner, riskPush, rnd, phRate)
            if code and text then
                phCaseId = string.format("PH-CASE-%03d.%03d-%02d-%s", dateInfo.year, dateInfo.dayOfYear, i, code)
                phText  = text
            else
                phCaseId = "—"
                phText  = "No concern"
            end
            -- restore RNG progression for subsequent steps
            math.randomseed(dateInfo.year*1000 + dateInfo.dayOfYear)
            -- reseed forward to where we were (cheap deterministic shim)
            for _=1,i*7 do math.random() end
         end
         end


Line 412: Line 520:
             local n = (notes or ""):lower()
             local n = (notes or ""):lower()
             if o:match("hold") or o:match("denial") or o:match("detention") or o:match("impound") or n:match("extended regulatory review") then
             if o:match("hold") or o:match("denial") or o:match("detention") or o:match("impound") or n:match("extended regulatory review") then
                 return ' style="background-color:#f8d7da;"' -- red
                 return ' style="background-color:#f8d7da;"'
             end
             end
             if o:match("administrative notice") or o:match("monitoring") or o:match("conditions") or o:match("reconciliation")
             if o:match("administrative notice") or o:match("monitoring") or o:match("conditions") or o:match("reconciliation")
               or n:match("wl%-ineligible") or n:match("risk tag: m%-amber") or n:match("surcharge advisory") then
               or n:match("wl%-ineligible") or n:match("risk tag: m%-amber") or n:match("surcharge advisory") then
                 return ' style="background-color:#fff3cd;"' -- yellow
                 return ' style="background-color:#fff3cd;"'
             end
             end
             return ' style="background-color:#d4edda;"' -- green
             return ' style="background-color:#d4edda;"'
         end)()
         end)()


         table.insert(out, "|-" .. style)
         table.insert(out, "|-" .. style)
         table.insert(out, string.format("| %s || %s || %s (%s) || %s || %s || %s || %s || %s || %s",
         if phOn then
             dateInfo.text, sector, vessel, flag.name, action, legal, outcome, appealCol, caseId, triggers, notes))
            table.insert(out, string.format("| %s || %s || %s (%s) || %s || %s || %s || %s || %s || %s || %s || %s",
                dateInfo.text, sector, vessel, flag.name, action, legal, outcome, appealCol, caseId, phCaseId, phText, triggers, notes))
        else
             table.insert(out, string.format("| %s || %s || %s (%s) || %s || %s || %s || %s || %s || %s",
                dateInfo.text, sector, vessel, flag.name, action, legal, outcome, appealCol, caseId, triggers, notes))
        end
     end
     end


Line 434: Line 547:
         table.insert(out, "<span style='background:#f8d7da;padding:2px 6px;border:1px solid #f5c6cb;'>Red</span> = Hold/Denial/Extended Review")
         table.insert(out, "<span style='background:#f8d7da;padding:2px 6px;border:1px solid #f5c6cb;'>Red</span> = Hold/Denial/Extended Review")
         table.insert(out, "<br/><i>Policy Stage (per flag):</i> I = Audit emphasis; II = +Market surcharge advisory; III = limited denial on high-risk, non-WL rows.")
         table.insert(out, "<br/><i>Policy Stage (per flag):</i> I = Audit emphasis; II = +Market surcharge advisory; III = limited denial on high-risk, non-WL rows.")
        table.insert(out, "</div>")
    end
    if showPHLeg and phOn then
        table.insert(out, "\n<div style='margin-top:0.3em; font-size:90%;'>")
        table.insert(out, "<b>Public Health Advisory Codes:</b> ")
        local parts = {}
        for _,d in ipairs(phDiseases) do
            table.insert(parts, string.format("<span style='white-space:nowrap;'><b>%s</b> – %s</span>", d.code, d.name))
        end
        table.insert(out, table.concat(parts, " &nbsp; "))
         table.insert(out, "</div>")
         table.insert(out, "</div>")
     end
     end

Revision as of 19:22, 28 September 2025

Documentation for this module may be created at Module:StraitsDaily/doc

-- Module: StraitsDaily
-- Purpose: Daily, in-world report of Strait of Haifa enforcement [[Straits Conventions of 52.06 PSSC]]


local p = {}

-------------------------------------------------------------------------------
-- (A) PSSC DATE
-------------------------------------------------------------------------------
local function getPSSCDate()
    local startDate = os.time({year = 1999, month = 8, day = 6})
    local secondsInDay = 86400
    local daysPerYear  = 183
    local now = os.time()
    local totalDaysElapsed = math.floor((now - startDate) / secondsInDay)

    local yearFraction = totalDaysElapsed / daysPerYear
    local psscYear     = math.floor(yearFraction)
    local dayOfYear    = math.floor((yearFraction - psscYear) * daysPerYear) + 1

    local month, dayOfMonth
    if dayOfYear <= 61 then
        month = 1; dayOfMonth = dayOfYear
    elseif dayOfYear <= 122 then
        month = 2; dayOfMonth = dayOfYear - 61
    else
        month = 3; dayOfMonth = dayOfYear - 122
    end

    local dateString = string.format("%d/%d/%d PSSC", dayOfMonth, month, psscYear)
    return { text = dateString, year = psscYear, dayOfYear = dayOfYear }
end

-------------------------------------------------------------------------------
-- (B) STATIC DATA
-------------------------------------------------------------------------------
local sectors = {
    "Strait South Inbound","Strait South Outbound","Strait North Inbound","Strait North Outbound",
    "Archipelago Zone","Sacred Buffer Approaches","Environmental Buffer","Northern Approach TSS","Southern Approach TSS"
}

local flags = {
    {name="Bassaridian-flag", weight=6, risky=false},
    {name="Moorland", weight=2, risky=false},
    {name="Imperial Federation", weight=5, risky=true},
    {name="Matamoros (IF)", weight=2, risky=true},
    {name="Neutral Merchant", weight=4, risky=false},
    {name="Diaspora Stakeholders- Euranidom", weight=3, risky=true},
    {name="Diaspora Stakeholders- Corumia", weight=3, risky=true},
    {name="Diaspora Stakeholders- South Keltiania", weight=3, risky=true},
    {name="Consortium Registry", weight=2, risky=true},
    {name="Independent Coastal State", weight=7, risky=false}
}

local vessels = {
    "Handymax bulker","Product tanker","General cargo ship","Container feeder",
    "Ro-Ro ferry","Research craft","Declared submarine","Undeclared submarine"
}

local cargoTypes = {
    {name="Medical reagents (dual-use precursors)", weight=3, risk=0.14, dual=true},
    {name="Industrial oxidizers / nitrates (dual-use)", weight=2, risk=0.16, dual=true},
    {name="Precision machine tools & CNC spares (dual-use)", weight=3, risk=0.15, dual=true},
    {name="Encrypted electronics & comms modules (dual-use)", weight=2, risk=0.17, dual=true},
    {name="Satellite components / sensors (dual-use)", weight=1, risk=0.18, dual=true},
    {name="Industrial fasteners (coded)", weight=2, risk=0.12, dual=true},
    {name="Hydraulic lubricants (dual-use)", weight=2, risk=0.13, dual=true},
    {name="High-frequency alloys (coded)", weight=1, risk=0.14, dual=true},
    {name="Aviation fuels & additives", weight=3, risk=0.12, dual=false},
    {name="Refined petroleum products", weight=4, risk=0.08, dual=false},
    {name="Relief supplies (mixed pallets)", weight=5, risk=0.06, dual=false},
    {name="Agricultural commodities", weight=5, risk=0.04, dual=false},
    {name="Finished consumer goods", weight=6, risk=0.03, dual=false}
}

local cargoTags = {
    {name="standard",  weight=6, risk=0.00},
    {name="expedited", weight=3, risk=0.05},
    {name="emergency", weight=2, risk=0.07},
    {name="perishable",weight=2, risk=0.06}
}

local partnerProfiles = {
    {code="P-07", weight=4, risk=0.00, recentLift=false},
    {code="P-12", weight=3, risk=0.03, recentLift=false},
    {code="P-17", weight=3, risk=0.12, recentLift=true},
    {code="P-22", weight=3, risk=0.14, recentLift=true},
    {code="P-31", weight=3, risk=0.05, recentLift=false},
    {code="P-38", weight=2, risk=0.10, recentLift=false},
    {code="P-44", weight=4, risk=0.02, recentLift=false},
    {code="P-53", weight=3, risk=0.04, recentLift=false}
}

-- Corum districts (match MissionaryDaily "Zones")
local corumDistricts = {
    {name="Lyvian Zones",     kind="NZ"},
    {name="Lucinarian Zones", kind="NZ"},
    {name="Brentonian Zones", kind="NZ"},
    {name="Ossyani Zones",    kind="NZ"},
    {name="Sinclarian Zones", kind="GM"},
    {name="Nortonian Zones",  kind="GM"},
    {name="Wallisian Zones",  kind="GM"},
    {name="Alindan Zones",    kind="GM"},
    {name="Aijiac Zones",     kind="GM"},
}
local districtKindByName = {}
for _,d in ipairs(corumDistricts) do districtKindByName[d.name] = d.kind end

-- White-Lane corridors tied to districts
local wlCorridors = {
    { code="WL-NZ-1", name="New Zimian Humanitarian Belt", win="0700–1100", kind="NZ",
      districts={"Lyvian Zones","Lucinarian Zones","Brentonian Zones","Ossyani Zones"} },
    { code="WL-GM-2", name="Bayen Relief Spur", win="0900–1300", kind="GM",
      districts={"Sinclarian Zones","Nortonian Zones","Wallisian Zones","Alindan Zones","Aijiac Zones"} },
    { code="WL-SEA-3", name="Corum Coastal Shuttle", win="0600–1800", kind="Mixed",
      districts={"Wallisian Zones","Nortonian Zones","Lyvian Zones"} }
}

local observers = {
    "Neutral temple auditor","Independent port compliance inspector",
    "Community elder (witness)","Straits Convention monitor (civilian)"
}

-- WL-03 participation pledge registry (true = signed; false = unsigned)
local wl03Signers = {
    ["Matamoros (IF)"] = false,        -- M-AMBER until pledge signed
    ["Imperial Federation"] = true,    -- signed/participating
    ["Bassaridian-flag"] = true,
    ["Neutral Merchant"] = true,
    ["Moorland"] = true,
    ["Independent Coastal State"] = true
}

-------------------------------------------------------------------------------
-- (B2) PUBLIC HEALTH: DISEASES & CONTROLS (same canon as outbreaks modules)
-------------------------------------------------------------------------------
local phDiseases = {
  {name="Thalassan Rot",        code="TR", controls="canal flush; rat-proofing; food-handler checks"},
  {name="Festival Blush",       code="FB", controls="ring vaccination; crowd spacing; dorm rotation"},
  {name="Pilgrim’s Flux",       code="PF", controls="boil notices; cistern audit; ORS distribution"},
  {name="Stygian Dust",         code="SD", controls="respirators; mist carts; guide clinic checks"},
  {name="Haifan Salt-Sickness", code="HS", controls="indoor rotations; antifungal clinics; dehumidify"},
  {name="Norsolyrian Drain",    code="ND", controls="lined ditches; praziquantel; snail abatement"},
  {name="Somniumpolis Lung",    code="SL", controls="dust suppression; respirator fit-checks"},
  {name="Haifan Crimson Fever", code="HCF",controls="larval control; dusk patrols; vector abatement"},
  {name="Strip Yellowing",      code="SY", controls="single-use ladles; vendor relicensing; recall"},
  {name="Eye of Styx",          code="ES", controls="eye-wash stations; visor compliance"},
  {name="Cato’s Curse",         code="CC", controls="PPE audits; solvent/oxidizer exposure controls"},
  {name="White Pestilence",     code="WP", controls="tiered quarantine; convoy assays; cordons"},
  {name="Breath of Nephele",    code="BN", controls="ventilation drills; mask catechism; spacing"},
  {name="Agnian Lockjaw",       code="AL", controls="tetanus boosters; injury clinics"},
}
local phIndex = {}
for _,d in ipairs(phDiseases) do phIndex[d.code] = d end

-- Decide if a row merits a PH advisory; return code and text or nil
local function healthConcern(sector, flag, vessel, cargo, tag, partner, riskPush, rnd, baseRate)
    -- baseRate is a small background chance when triggers are weak (default 0.35)
    local score = 0
    -- Sector effects (coastal, sacred, environmental buffers)
    if sector == "Environmental Buffer" or sector == "Sacred Buffer Approaches"
       or sector:find("Strait") or sector:find("Approach") then
        score = score + 1
    end
    -- Cargo/tag effects
    local cname = cargo and cargo.name or ""
    local tname = tag and tag.name or "standard"
    if cname == "Agricultural commodities" or tname == "perishable" then score = score + 2 end
    if cname == "Relief supplies (mixed pallets)" then score = score + 1 end
    if cname == "Aviation fuels & additives" or cname == "Refined petroleum products"
       or cname == "Hydraulic lubricants (dual-use)" or cname:find("oxidizers") then score = score + 2 end
    -- Crowding signals (ferries, expedited/emergency)
    if vessel == "Ro-Ro ferry" and (tname == "expedited" or tname == "emergency") then score = score + 2 end
    -- Partner/flag risk
    if (partner and partner.risk or 0) >= 0.09 then score = score + 1 end
    if flag and flag.risky then score = score + 0.5 end
    -- Enforcement multi-trigger
    score = score + (riskPush or 0) * 0.5

    -- Choose disease based on dominant trigger theme
    local function pick(code)
        local d = phIndex[code]
        if not d then return nil end
        return d.code, (d.code .. " ("..d.name.."): " .. d.controls)
    end

    -- Rare escalation: White Pestilence screening on very high scores
    if score >= 5 and rnd() < 0.10 then
        return pick("WP")
    end

    -- Food/water hygiene path
    if (cname == "Agricultural commodities" or tname == "perishable") and rnd() < 0.75 then
        return pick("SY")
    end
    if (cname == "Relief supplies (mixed pallets)" or score >= 3) and rnd() < 0.40 then
        return pick("PF")
    end

    -- Waterborne/coastal path
    if (sector == "Environmental Buffer" or sector:find("Strait")) and rnd() < 0.50 then
        return pick("TR")
    end
    if sector == "Archipelago Zone" and rnd() < 0.30 then
        return pick("HS")
    end

    -- Solvent/oxidizer/industrial exposure
    if (cname:find("oxidizers") or cname:find("lubricants") or cname:find("petroleum") or cname:find("fuels")) and rnd() < 0.70 then
        return pick("CC")
    end

    -- Crowd respiratory
    if vessel == "Ro-Ro ferry" and (tname == "expedited" or tname == "emergency") and rnd() < 0.60 then
        return pick("FB")
    end
    if partner and partner.risk >= 0.09 and rnd() < 0.30 then
        return pick("BN")
    end

    -- Low background chance
    if rnd() < (baseRate or 0.35) then
        -- pick a mild, generic hygiene signal weighted toward SY/PF
        local r = rnd()
        if r < 0.45 then return pick("SY")
        elseif r < 0.70 then return pick("PF")
        elseif r < 0.85 then return pick("TR")
        else return pick("BN") end
    end

    return nil
end

-------------------------------------------------------------------------------
-- (C) POLICY STAGES (per-flag mapping)
-------------------------------------------------------------------------------
local stageMap = {
    ["Imperial Federation"] = 1,  -- Stage I (audit emphasis)
    ["Matamoros (IF)"]      = 3,  -- Stage III (limited denial on high-risk, non-WL rows)
}
local function getFlagStage(flagName, globalStage)
    local s = stageMap[flagName]
    if s ~= nil then return s end
    return globalStage or 0
end

-------------------------------------------------------------------------------
-- (D) HELPERS
-------------------------------------------------------------------------------
local function pick(list) return list[math.random(#list)] end
local function pickWeighted(list)
    local total = 0
    for _, item in ipairs(list) do total = total + (item.weight or 1) end
    local roll, cumul = math.random(total), 0
    for _, item in ipairs(list) do
        cumul = cumul + (item.weight or 1)
        if roll <= cumul then return item end
    end
    return list[#list]
end

-- WL eligibility (Annex G) + WL-03 participation + 90-day cooldown
local function wlEligibility(cargo, tag, flag, partner)
    if not cargo or not tag then return false, "n/a" end
    local green = (cargo.name == "Relief supplies (mixed pallets)" or cargo.name == "Agricultural commodities")
    local amber = (tag.name == "expedited" or tag.name == "emergency" or tag.name == "perishable")
    local baseEligible = ((green or amber) and not cargo.dual)

    local signed = wl03Signers[flag.name]
    if signed == nil then signed = true end
    local cooling = (partner and partner.recentLift) or false

    if not baseEligible then
        return false, "Not eligible cargo/tag"
    elseif not signed then
        return false, "WL-03 not signed (M-AMBER)"
    elseif cooling then
        return false, "WL-03 cooldown (90 days)"
    else
        return true, "WL-03 compliant"
    end
end

local function legalBasis(sector, wl, wl03note)
    if wl then
        return "Art III.5 (White-Lane); Annex G; Art X.3 (expedited remedies); HCE Circular WL-03 (" .. wl03note .. ")"
    end
    if sector == "Sacred Buffer Approaches" or sector == "Environmental Buffer" then
        return "Art III.2.3 (Sacred quiet zones); Annex A (delims); Art VII; Annex D"
    end
    return "Art IV.1–1.2 (PSC); Annex F (SCT/SAC procedures)"
end

local function appealsStatus(sector, wl, tag, cargo)
    if wl then
        local relief = 0.60
        if tag and (tag.name=="emergency" or tag.name=="perishable") then relief = relief + 0.12 end
        if cargo and cargo.dual then relief = relief - 0.15 end
        local r = math.random()
        local outcome = (r <= relief * 0.6) and "clearance granted"
                      or (r <= relief)       and "expedited with conditions"
                      or "conditions imposed"
        return string.format("HCE Review → SCT (72h) → SAC (≤5d) — %s (Art X.3; Annex G; WL-03 liaison)", outcome)
    else
        local level = (sector=="Sacred Buffer Approaches" or sector=="Environmental Buffer") and "SAC Referral"
                      or ((math.random()<0.5) and "SCT Admin Review" or "PSC reconciliation")
        local outcome = (level=="PSC reconciliation") and "notice acknowledged"
                        or ((math.random()<0.5) and "clearance granted" or "conditions imposed")
        local cite = (level=="SAC Referral") and "Annex F (SAC)" or "Annex F (SCT)"
        return string.format("%s — %s (%s)", level, outcome, cite)
    end
end

local function hceRef(dateInfo, corridor)
    return string.format("HCE-%03d.%03d-%s", dateInfo.year, dateInfo.dayOfYear, corridor.code)
end

local function pickServedDistrict(corr)
    local dname = corr.districts[math.random(#corr.districts)]
    local kind  = districtKindByName[dname] or "NZ"
    return dname, kind
end

local function buildTriggers(flag, sector, cargo, tag, partner, wlEligible, wl03note, riskPush, flagStage)
    local t = {}
    if flag.risky then table.insert(t, "risky flag") end
    if sector == "Sacred Buffer Approaches" or sector == "Environmental Buffer" then
        table.insert(t, "sacred/ecology buffer")
    end
    if cargo and cargo.dual then table.insert(t, "dual-use cargo") end
    if cargo and (cargo.risk or 0) >= 0.12 then table.insert(t, "cargo risk ≥0.12") end
    if tag and tag.name ~= "standard" then table.insert(t, "non-standard tag: "..tag.name) end
    if partner and (partner.risk or 0) >= 0.09 then table.insert(t, "partner risk ≥0.09") end
    if wlEligible then table.insert(t, "WL pre-cleared") end
    if wl03note == "WL-03 not signed (M-AMBER)" then table.insert(t, "WL-03 unsigned (M-AMBER)") end
    if wl03note == "WL-03 cooldown (90 days)" then table.insert(t, "WL-03 cooldown") end
    if flagStage and flagStage > 0 then table.insert(t, "policy stage "..flagStage) end
    if riskPush and riskPush >= 2 then table.insert(t, "multi-trigger escalation") end
    if #t == 0 then return "none" end
    return table.concat(t, "; ")
end

-------------------------------------------------------------------------------
-- (E) MAIN GENERATION
-------------------------------------------------------------------------------
function p.makeTable(frame)
    local dateInfo = getPSSCDate()
    math.randomseed(dateInfo.year*1000 + dateInfo.dayOfYear)

    local rows        = tonumber(frame and frame.args and frame.args.rows) or 18
    local globalStage = tonumber(frame and frame.args and frame.args.policy_stage) or 0
    local showLegend  = (frame and frame.args and tostring(frame.args.showlegend) or "yes"):lower() ~= "no"

    -- Public health toggles
    local phOn        = (frame and frame.args and tostring(frame.args.ph) or "on"):lower() ~= "off"
    local phRate      = tonumber(frame and frame.args and frame.args.phrate) or 0.35
    local showPHLeg   = (frame and frame.args and tostring(frame.args.showphlegend) or "yes"):lower() ~= "no"

    local out = {}
    table.insert(out, "{| class='wikitable sortable'")
    if phOn then
        table.insert(out, "! Date !! Sector !! Vessel/Flag !! Action !! Legal Basis !! Outcome !! Appeals/Review (Straits Conventions) !! Case ID !! PH Case !! Public Health Advisory !! Triggers !! Notes")
    else
        table.insert(out, "! Date !! Sector !! Vessel/Flag !! Action !! Legal Basis !! Outcome !! Appeals/Review (Straits Conventions) !! Case ID !! Triggers !! Notes")
    end

    for i = 1, rows do
        local sector  = pick(sectors)
        local flag    = pickWeighted(flags)
        local vessel  = pick(vessels)
        local cargo   = pickWeighted(cargoTypes)
        local tag     = pickWeighted(cargoTags)
        local partner = pickWeighted(partnerProfiles)

        local wlOK, wl03note = wlEligibility(cargo, tag, flag, partner)
        local wlEligible = wlOK
        local corridor = wlEligible and wlCorridors[math.random(#wlCorridors)] or nil
        local obs = wlEligible and observers[math.random(#observers)] or nil

        -- Per-flag stage (overrides global when present)
        local flagStage = getFlagStage(flag.name, globalStage)

        -- Base enforcement probability
        local function baseProb()
            local q = 0.28
            if flag.risky then q = q + 0.06 end
            if sector == "Sacred Buffer Approaches" or sector == "Environmental Buffer" then q = q + 0.08 end
            q = q + (cargo and cargo.risk or 0)
            if cargo and cargo.dual then q = q + 0.03 end
            q = q + (tag and tag.risk or 0)
            q = q + (partner and partner.risk or 0)
            if wlEligible then q = math.max(q - 0.06, 0.10) end

            -- Per-flag policy stages apply to non-WL rows only
            local isIF = flag.name == "Imperial Federation"
            local isMT = flag.name:match("^Matamoros")
            if (isIF or isMT) and not wlEligible then
                if flagStage >= 1 then q = q + 0.04 end
                if flagStage >= 2 then q = q + 0.04 end
            end

            -- M-AMBER effect
            if flag.name:match("Matamoros") and wl03note == "WL-03 not signed (M-AMBER)" then
                q = q + 0.08
            end

            -- Bassaridian-flag compliance
            if flag.name == "Bassaridian-flag" then
                q = q - 0.18
                if wlEligible then q = q - 0.04 end
                if q < 0.02 then q = 0.02 end
                if q > 0.08 then q = 0.08 end
            end
            return math.min(q, 0.72)
        end

        local enforce = baseProb()
        local action, legal, outcome, notes, appealCol
        local dname, dkind

        if wlEligible then
            dname, dkind = pickServedDistrict(corridor)
            action = "WL transit / pilotage verification"
            legal  = legalBasis(sector, true, wl03note)
            notes  = string.format(
                "WL %s (%s) | window %s | District: %s [%s] | Observer: %s | %s | Cargo: %s [%s]; Partner: %s | WL-03: %s",
                corridor.code, corridor.name, corridor.win, dname, dkind, (obs or "n/a"),
                hceRef(dateInfo, corridor), cargo.name, tag.name, partner.code, wl03note
            )
        else
            action = (wl03note == "WL-03 not signed (M-AMBER)" or wl03note == "WL-03 cooldown (90 days)")
                     and "WL-ineligible: standard PSC applied"
                     or  "Cargo manifest reconciliation audit"
            legal  = legalBasis(sector, false, wl03note)

            local advisory = ""
            local isIF = flag.name == "Imperial Federation"
            local isMT = flag.name:match("^Matamoros")
            if (isIF or isMT) and flagStage >= 1 then
                advisory = " | Audit emphasis (Stage I)"
                if flagStage >= 2 then advisory = advisory .. " | Market surcharge advisory (Stage II)" end
            end

            local mAmber = (flag.name:match("Matamoros") and wl03note == "WL-03 not signed (M-AMBER)") and " | Risk tag: M-AMBER" or ""
            notes  = string.format("Cargo: %s [%s]; Partner: %s | WL-03: %s%s%s",
                                   cargo.name, tag.name, partner.code, wl03note, mAmber, advisory)
        end

        -- Compute enforcement result
        local riskPush = 0
        if flag.name:match("Imperial Federation") or flag.name:match("^Matamoros") then riskPush = riskPush + 1 end
        if cargo.dual or (cargo.risk or 0) >= 0.12 then riskPush = riskPush + 1 end
        if tag.name ~= "standard" then riskPush = riskPush + 1 end
        if (partner.risk or 0) >= 0.09 then riskPush = riskPush + 1 end
        if wlEligible then riskPush = math.max(riskPush - 1, 0) end

        if math.random() > enforce then
            outcome  = wlEligible and "Cleared WL window" or "No reports"
            appealCol = wlEligible and "N/A (WL cleared)" or "N/A"
        else
            outcome = wlEligible and "WL reconciliation check; monitoring" or "Administrative notice issued; monitoring"
            if riskPush >= 2 and math.random() < 0.65 then
                outcome = (wlEligible and "WL hold; clearance pending expedited review" or "Random audit hold; clearance pending")
                if flag.name == "Imperial Federation" or flag.name:match("^Matamoros") then
                    outcome = outcome .. " [Extended regulatory review]"
                end
            end

            -- Stage III denial logic
            local isIF = flag.name == "Imperial Federation"
            local isMT = flag.name:match("^Matamoros")
            if flagStage >= 3 and (isIF or isMT) and not wlEligible and riskPush >= 2 then
                outcome  = "Denial of passage (policy Stage III)"
                appealCol = "SCT injunction eligible | Appeals Case pending"
            end

            -- Bassaridian-flag safeguard
            if flag.name == "Bassaridian-flag" and outcome:match("hold") then
                outcome  = wlEligible and "WL reconciliation complete; cleared same window" or "Administrative reconciliation complete; cleared"
                appealCol = wlEligible and "N/A (WL cleared)" or "N/A"
            end

            appealCol = appealCol or appealsStatus(sector, wlEligible, tag, cargo)
        end

        -- Public Health advisory (deterministic per row)
        local phCaseId, phText = "", ""
        if phOn then
            -- stabilize PH random draws per-row by reseeding on top of the daily seed
            local rowSeed = (dateInfo.year*1000 + dateInfo.dayOfYear) + i*131
            math.randomseed(rowSeed)
            local function rnd() return math.random() end
            local code, text = healthConcern(sector, flag, vessel, cargo, tag, partner, riskPush, rnd, phRate)
            if code and text then
                phCaseId = string.format("PH-CASE-%03d.%03d-%02d-%s", dateInfo.year, dateInfo.dayOfYear, i, code)
                phText   = text
            else
                phCaseId = "—"
                phText   = "No concern"
            end
            -- restore RNG progression for subsequent steps
            math.randomseed(dateInfo.year*1000 + dateInfo.dayOfYear)
            -- reseed forward to where we were (cheap deterministic shim)
            for _=1,i*7 do math.random() end
        end

        -- Transparency fields
        local caseId = string.format("CASE-%03d.%03d-%02d", dateInfo.year, dateInfo.dayOfYear, i)
        local triggers = buildTriggers(flag, sector, cargo, tag, partner, wlEligible, wl03note, riskPush, flagStage)

        -- If there is any reviewable action, bind an appeals case stub
        if outcome:lower():match("hold") or outcome:lower():match("conditions") or outcome:lower():match("monitoring") or outcome:lower():match("denial") then
            appealCol = (appealCol ~= "N/A" and (appealCol .. " | ")) or ""
            appealCol = appealCol .. ("Appeals Case: APP-" .. caseId)
        end

        -- Color-code the row and output
        local style = (function()
            local o = outcome:lower()
            local n = (notes or ""):lower()
            if o:match("hold") or o:match("denial") or o:match("detention") or o:match("impound") or n:match("extended regulatory review") then
                return ' style="background-color:#f8d7da;"'
            end
            if o:match("administrative notice") or o:match("monitoring") or o:match("conditions") or o:match("reconciliation")
               or n:match("wl%-ineligible") or n:match("risk tag: m%-amber") or n:match("surcharge advisory") then
                return ' style="background-color:#fff3cd;"'
            end
            return ' style="background-color:#d4edda;"'
        end)()

        table.insert(out, "|-" .. style)
        if phOn then
            table.insert(out, string.format("| %s || %s || %s (%s) || %s || %s || %s || %s || %s || %s || %s || %s",
                dateInfo.text, sector, vessel, flag.name, action, legal, outcome, appealCol, caseId, phCaseId, phText, triggers, notes))
        else
            table.insert(out, string.format("| %s || %s || %s (%s) || %s || %s || %s || %s || %s || %s",
                dateInfo.text, sector, vessel, flag.name, action, legal, outcome, appealCol, caseId, triggers, notes))
        end
    end

    table.insert(out, "|}")
    if showLegend then
        table.insert(out, "\n<div style='margin-top:0.5em; font-size:90%;'>")
        table.insert(out, "<b>Legend:</b> ")
        table.insert(out, "<span style='background:#d4edda;padding:2px 6px;border:1px solid #c3e6cb;'>Green</span> = Cleared/Compliant &nbsp; ")
        table.insert(out, "<span style='background:#fff3cd;padding:2px 6px;border:1px solid #ffeeba;'>Yellow</span> = Administrative/Conditional/Monitoring &nbsp; ")
        table.insert(out, "<span style='background:#f8d7da;padding:2px 6px;border:1px solid #f5c6cb;'>Red</span> = Hold/Denial/Extended Review")
        table.insert(out, "<br/><i>Policy Stage (per flag):</i> I = Audit emphasis; II = +Market surcharge advisory; III = limited denial on high-risk, non-WL rows.")
        table.insert(out, "</div>")
    end
    if showPHLeg and phOn then
        table.insert(out, "\n<div style='margin-top:0.3em; font-size:90%;'>")
        table.insert(out, "<b>Public Health Advisory Codes:</b> ")
        local parts = {}
        for _,d in ipairs(phDiseases) do
            table.insert(parts, string.format("<span style='white-space:nowrap;'><b>%s</b> – %s</span>", d.code, d.name))
        end
        table.insert(out, table.concat(parts, " &nbsp; "))
        table.insert(out, "</div>")
    end

    return table.concat(out, "\n")
end

return p