Module:AutoUpdateAgPrices

From MicrasWiki
Revision as of 18:32, 29 December 2025 by NewZimiaGov (talk | contribs)
Jump to navigationJump to search

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

---------------------------------------------------------
-- Module:AutoUpdateAgPrices
-- Generates daily-updated agricultural-sector prices.
--
-- Features:
--  • Seeds randomness each day using localTime.year + localTime.yday
--  • Tier prices derived from the final adjusted price
--  • Minor event-based price boost for special days
--  • NEW (Dec 2025): Condensed display (Tier I headline) + collapsible full breakdown
--  • NEW (Dec 2025): Δ symbol column (↑ Low / ↓ High / • Standard)
--  • NEW (Dec 2025): Modes so editors can force full expanded output when updating
--
-- Usage:
--   {{#invoke:AutoUpdateAgPrices|generateTable}}
--     - default: summary + collapsed breakdown
--   {{#invoke:AutoUpdateAgPrices|generateTable|mode=summary}}
--     - summary only
--   {{#invoke:AutoUpdateAgPrices|generateTable|mode=breakdown}}
--     - breakdown only (collapsed by default)
--   {{#invoke:AutoUpdateAgPrices|generateTable|mode=breakdown|collapse=no}}
--     - breakdown only (expanded)
--   {{#invoke:AutoUpdateAgPrices|full}}
--     - breakdown only (expanded) convenience function
---------------------------------------------------------

local p = {}

---------------------------------------------------------
-- 1. getCurrentDate
--    Determines today's PSSC date and returns:
--      * dateString in "day/month/year [[PSSC]]"
--      * dayOfMonth (1..61)
--      * month (1=Atosiel, 2=Thalassiel, 3=Opsitheiel)
---------------------------------------------------------
local function getCurrentDate()
	local startDate = os.time({ year = 1999, month = 8, day = 6 })
	local secondsInDay = 86400
	local daysPerYear = 183
	local currentDate = os.time()
	local totalDaysElapsed = math.floor((currentDate - 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

	return string.format("%d/%d/%d [[PSSC]]", dayOfMonth, month, psscYear), dayOfMonth, month
end

---------------------------------------------------------
-- 2. Product Data
---------------------------------------------------------
local products = {
	{ "Rice", "Gladeseed Farmers' Union", 9.00, "tons" },
	{ "Edible Algae", "Gladeseed Farmers' Union", 21.00, "tons" },
	{ "Shrimp", "Gladeseed Farmers' Union", 15.00, "tons" },
	{ "Anterran Spicy Onion", "Gladeseed Farmers' Union", 18.00, "tons" },
	{ "Queen's Garlic", "Gladeseed Farmers' Union", 27.00, "tons" },
	{ "Morovian Water Buffalo", "Atterian Cattle Drivers", .60, "head" },
	{ "Morovian Water Buffalo Beef", "Atterian Cattle Drivers", 5, "tons" },
	{ "Unprocessed Noctic-Rabrev Leaf Concentrate", "Noctic Fleet", .60, "barrels" },
	{ "Processed Noctic-Rabrev Leaf Concentrate", "Noctic Fleet", .30, "barrels" },
	{ "Dried Noctic-Rabrev Petals", "Noctic Fleet", 2.25, "kilograms" },
	{ "Noctic-Rabrev Flower Oil Extract", "Noctic Fleet", .450, "liters" },
	{ "Noctic-Rabrev Culinary Spice Blend", "Noctic Fleet", 1.75, "kilograms" },
	{ "Sanctified Noctic Leaf (ritual-grade)", "Noctic Fleet", 9.00, "bundles" },
	{ "Noctic Tincture (therapeutic dosage)", "Noctic Fleet", 2.50, "vials" },
	{ "Noctic-Rabrev Infused Incense", "Noctic Fleet", 3.00, "packs" },
	{ "Noctic Flower Lokum (sweet export)", "Noctic Fleet", 2.25, "boxes" },
	{ "Temple-Pressed Noctic Resin", "Noctic Fleet", 5.00, "bricks" },
	{ "Distilled Noctic Elixir (medical grade)", "Noctic Fleet", 6.75, "bottles" },
	{ "Noctic-Rabrev Anointing Balm", "Noctic Fleet", 4.00, "jars" },
	{ "Noctic-Rabrev Ritual Ink", "Noctic Fleet", 3.50, "bottles" },
	{ "Noctic Soak Oil", "Noctic Fleet", 4.75, "flasks" },
	{ "Nocticine Silk-Wrap Cigarettes (20-count)", "Noctic Fleet", 7.25, "packs" },
	{ "Caravan Reserve Noctic Cigars (hand-rolled)", "Noctic Fleet", 3.40, "sticks" },
	{ "Pocket-Press Noctic Chew Pouches (minted)", "Noctic Fleet", 4.80, "tins" },
	{ "Chewable Night-Leaf Gum (sugar-free)", "Noctic Fleet", 3.20, "blisters" },
	{ "Quick-Fuse Nocticine Buccal Film", "Noctic Fleet", 8.10, "sheets" },
	{ "Hand-Cured Nefel Leaf Cigarettes (20-count)", "Nefel Village", 6.80, "packs" },
	{ "Village Reserve Tobacco Cigars (oak-aged)", "Nefel Village", 4.10, "sticks" },
	{ "Pressed Nefel Chew Rolls (spiced)", "Nefel Village", 4.50, "tins" },
	{ "Sun-Dried Pipe Tobacco (long-cut)", "Nefel Village", 5.25, "pouches" },
	{ "Tobacco Infused Oil (for incense burners)", "Nefel Village", 3.90, "flasks" },
	{ "Nefel Clay-Smoked Cigarettes (10-count)", "Nefel Village", 7.45, "packs" },
	{ "Herbal-Blend Hookah Tobacco", "Nefel Village", 6.20, "jars" },
	{ "Tobacco Ash Charcoal Tablets (ritual use)", "Nefel Village", 2.60, "discs" },
	{ "Manwine", "Jogiani Merchants of Manwine", 2.07, "barrels" },
	{ "Milkwine", "Jogiani Merchants of Manwine", 6.00, "barrels" },
	{ "Zoe Elm Resin (Unrefined)", "Keepers of the Grove of Zoe Elm", 0.03, "barrels" },
	{ "Zoe Elm Resin (Refined)", "Keepers of the Grove of Zoe Elm", 0.04, "barrels" },
	{ "Sea Elm Flavored Mead", "Sea-Elm Southern Meadery", 1.11, "barrels" },
	{ "Wheat", "Plains of Jogi", 0.30, "tons" },
	{ "Oats", "Plains of Jogi", 0.23, "tons" },
	{ "Cereal", "Plains of Jogi", 0.52, "pallets" },
	{ "Hay", "Plains of Jogi", 0.45, "bales" },
	{ "Ale of the Night", "Ale of the Night Central Brewery", 2.43, "barrels" },
	{ "Camels", "Camel Herders of the Jangsong Province", 1.04, "head" },
	{ "Camel Meat", "Camel Herders of the Jangsong Province", 34.65, "tons" },
	{ "Camel Milk", "Camel Herders of the Jangsong Province", 5.20, "barrels" },
	{ "Camel Hump Water", "Camel Herders of the Jangsong Province", 3.47, "barrels" },
	{ "Camel Hump Water Tea", "Camel Herders of the Jangsong Province", 0.86, "barrels" },
	{ "Pearls", "Anterran Imports and Services", 0.79, "sacks" },
	{ "Abalone", "Anterran Imports and Services", 2.64, "crates" },
	{ "Opium", "Merchant Guild of the Poppy Goddess", 0.18, "sacks" },
	{ "Hash", "Merchant Guild of the Poppy Goddess", 0.17, "sacks" },
	{ "Mead", "Grand Cave Bee Meadery", 0.49, "barrels" },
	{ "Cinammon", "Cinammon Plains of Roue", 0.45, "tons" },
	{ "Velvet Worm Meat", "The Velvet Company", 1.04, "tons" },
	{ "Velvet Worm Eggs", "The Velvet Company", 0.35, "crates" },
	{ "Silk", "Seamstresses of Rouge", 5.00, "rolls" },
	{ "Horehound Mead", "Salurian Temple of Sacred Horehound", 0.05, "barrels" },
	{ "Horehound Beer", "Salurian Temple of Sacred Horehound", 0.07, "barrels" },
	{ "Sea Cow Beef", "Bulhanu Ranchers' Association", 8.50, "tons" },
	{ "Atterian Armored Pufferfish", "Suncliff Fisheries", 3.00, "tons" },
	{ "Morovian Saucer Eyed Giant Eel", "Suncliff Fisheries", 7.50, "tons" },
	{ "Amina Crab", "Suncliff Fisheries", 6.00, "tons" },
	{ "Mugwort Oil", "Ministers of Mistress Mugwort", 0.35, "barrels" },
	{ "Ground Mugwort", "Ministers of Mistress Mugwort", 0.75, "sacks" },
	{ "Mugwort Tincture", "Ministers of Mistress Mugwort", 0.10, "barrels" },
	{ "Blood InfusedRed Wine", "Blood Vineyards of the Far North", 0.39, "barrels" },
	{ "Wisp Rice", "Norsolyrian Wisp Rice Farmers Association", 15.00, "tons" },
	{ "Giid-Lisea-Eda", "Giid-Lisea Ranching Import Company", 0.41, "head" },
	{ "Giid-Lisea-Eda Beef", "Giid-Lisea Ranching Import Company", 7.10, "tons" },
	{ "Giid-Lisea-Eda Milk", "Giid-Lisea Ranching Import Company", 3.85, "liters" },
	{ "Cream Butter", "Giid-Lisea Ranching Import Company", 2.12, "pounds" },
	{ "Aged Cheese", "Giid-Lisea Ranching Import Company", 1.44, "wheels" },
	{ "Giid-Lisea Hide Leather", "Giid-Lisea Ranching Import Company", 4.23, "sheets" },
	{ "Tusk Ivory Carvings", "Giid-Lisea Ranching Import Company", 0.73, "pieces" },
	{ "Bone-Broth Concentrate", "Giid-Lisea Ranching Import Company", 5.67, "jars" },
	{ "Mammoth Fat Candles", "Giid-Lisea Ranching Import Company", 1.98, "candles" },
	{ "Frozen Mammoth Steaks", "Giid-Lisea Ranching Import Company", 6.22, "pounds" },
	{ "Pickled Mammoth Tongue", "Giid-Lisea Ranching Import Company", 0.89, "jars" },
	{ "Giid-Lisea Jerky (salted)", "Giid-Lisea Ranching Import Company", 4.56, "slabs" },
	{ "Mammoth Bone Flour (fertilizer)", "Giid-Lisea Ranching Import Company", 2.71, "sacks" },
	{ "Giid-Lisea Cream Liqueur", "Giid-Lisea Ranching Import Company", 3.14, "bottles" },
	{ "Highland Ale (milk-based)", "Giid-Lisea Ranching Import Company", 5.09, "tankards" },
	{ "Vaeringheim Turkey Meat", "Bird Keepers of Saluria", 1.07, "tons" },
	{ "Salurian Swamp Quail Eggs", "Bird Keepers of Saluria", 12.00, "crates" },
	{ "Giant Fanged Penguin Blubber", "Hunters of the Giant Fanged Penguin", 25.00, "tons" },
	{ "Giant Fanged Penguin Blubber Oil Extract", "Hunters of the Giant Fanged Penguin", 5.00, "barrels" },
	{ "Coffee", "Aminian Coffee Company", 100, "pounds" },
	{ "Reindeer Meat", "Knights Castle Reindeer Herders", 3.15, "tons" },
	{ "Leather", "Knights Castle Reindeer Herders", 7.5, "pounds" },
	{ "Bone Products", "Knights Castle Reindeer Herders", 1.06, "crates" },
	{ "Whale Meat", "Lindley Whaling Company", 13.5, "tons" },
	{ "Whale Oil", "Lindley Whaling Company", .77, "crates" },
	{ "Whale Bone", "Lindley Whaling Company", 5.1, "tons" },
	{ "Seal Meat", "Lindley Whaling Company", 3.1, "tons" },
	{ "Seal Oil", "Lindley Whaling Company", .32, "crates" },
	{ "Seal Fur", "Lindley Whaling Company", 2.23, "crates" },
	{ "Sea Lion Meat", "Lindley Whaling Company", 6.45, "tons" },
	{ "Sea Lion Oil", "Lindley Whaling Company", .46, "crates" },
	{ "Sea Lion Fur", "Lindley Whaling Company", 4.8, "crates" },
	{ "Beans", "Granadita Collective", 1.8, "tons" },
	{ "Potatos", "Granadita Collective", 4.9, "tons" },
	{ "Squash", "Granadita Collective", 4.7, "tons" },
	{ "Wheat", "Granadita Collective", 1.13, "tons" },
	{ "maize", "Granadita Collective", 5.82, "tons" },
	{ "Vegetable Lamb of Tartary", "Herd Caton", 2.77, "head" },
	{ "Lamb’s Wool (Vegetable Blend)", "Herd Caton", .18, "kg" },
	{ "Lamb-Root Cheese", "Herd Caton", .42, "wheels" },
	{ "Tartary Saplings", "Herd Caton", .32, "plants" },
	{ "Pickled Lamb Stalks", "Herd Caton", .24, "jars" },
	{ "Partially Digested Fish Pulp", "Wasp Hunters of the Austral Isles", 23.76, "pounds" },
	{ "Fermented Fish Pulp Bricks", "Wasp Hunters of the Austral Isles", 4.12, "bricks" },
	{ "Fish Gut Oil (cooking & lamp use)", "Wasp Hunters of the Austral Isles", 7.85, "liters" },
	{ "Salted Intestinal Mash", "Wasp Hunters of the Austral Isles", 15.63, "pounds" },
	{ "Brined Gill Ribbons", "Wasp Hunters of the Austral Isles", 3.21, "bundles" },
	{ "Smoked Aiji-Hial-Nas Strips", "Wasp Hunters of the Austral Isles", 8.44, "pounds" },
	{ "Pickled Stomach Chunks", "Wasp Hunters of the Austral Isles", 2.09, "jars" },
	{ "Fish Maw Jelly", "Wasp Hunters of the Austral Isles", 6.39, "jars" },
	{ "Dried Scale Powder (seasoning)", "Wasp Hunters of the Austral Isles", 2.44, "sachets" },
	{ "Sea-Salt Ferment Porridge", "Wasp Hunters of the Austral Isles", 5.70, "bowls" },
	{ "Wasp Hunter’s Black Rum", "Wasp Hunters of the Austral Isles", 9.80, "bottles" },
	{ "Fish-Pulp Mead (sweet & briny blend)", "Wasp Hunters of the Austral Isles", 4.55, "bottles" },
	{ "Spiced Intestine Ale", "Wasp Hunters of the Austral Isles", 6.13, "tankards" },
	{ "Dried Roe Biscuits", "Wasp Hunters of the Austral Isles", 3.22, "cakes" },
	{ "Hunter’s Brine Wine (fortified)", "Wasp Hunters of the Austral Isles", 7.90, "bottles" },
	{ "Raw Cane Sugar", "Diamonia Sugar Cooperatives", 1.92, "tons" },
	{ "Refined White Sugar", "Diamonia Sugar Cooperatives", 1.44, "tons" },
	{ "Brown Sugar", "Diamonia Sugar Cooperatives", 0.88, "tons" },
	{ "Molasses", "Diamonia Sugar Cooperatives", 0.76, "barrels" },
	{ "Rock Sugar Crystals", "Diamonia Sugar Cooperatives", 0.67, "crates" },
	{ "Sugar Loaves", "Diamonia Sugar Cooperatives", 1.15, "blocks" },
	{ "Date Sugar Blend", "Diamonia Sugar Cooperatives", 0.38, "sacks" },
	{ "Candied Citrus Peel", "Diamonia Sugar Cooperatives", 0.24, "crates" },
	{ "Sugar Syrup", "Diamonia Sugar Cooperatives", 0.91, "barrels" },
	{ "Sweet Tea Cakes", "Diamonia Sugar Cooperatives", 0.53, "boxes" },
	{ "Cold-Steppe Barley", "Ouriana Lowlands Farmers' Association", 8.25, "tons" },
	{ "Proso Millet", "Ouriana Lowlands Farmers' Association", 7.75, "tons" },
	{ "Buckwheat", "Ouriana Lowlands Farmers' Association", 9.50, "tons" },
	{ "Foxtail Millet", "Ouriana Lowlands Farmers' Association", 8.00, "tons" },
	{ "Cold-Hardy Lentils", "Ouriana Lowlands Farmers' Association", 12.00, "tons" },
	{ "Turnips (Mountain-Grown)", "Ouriana Lowlands Farmers' Association", 6.25, "tons" },
	{ "Rutabagas (Highland)", "Ouriana Lowlands Farmers' Association", 6.50, "tons" },
	{ "Alpine Cabbage", "Ouriana Lowlands Farmers' Association", 6.75, "tons" },
	{ "Wild Rye", "Ouriana Lowlands Farmers' Association", 9.00, "tons" },
	{ "Cultivated Rye", "Ouriana Lowlands Farmers' Association", 9.75, "tons" },
	{ "Kohlrabi", "Ouriana Lowlands Farmers' Association", 7.25, "tons" },
	{ "Steppe Rhubarb", "Ouriana Lowlands Farmers' Association", 5.75, "tons" },
	{ "Mountain Sorrel", "Ouriana Lowlands Farmers' Association", 4.00, "tons" },
	{ "Cold-Hardy Hops", "Ouriana Lowlands Farmers' Association", 18.00, "tons" },
	{ "Highland Herbal Blend (raw)", "Ouriana Lowlands Farmers' Association", 10.50, "tons" },
	{ "Fermenting Barley", "Ouriana Lowlands Farmers' Association", 12.75, "tons" },
	{ "Highland Lentil and Millet Mix (packaged)", "Ouriana Lowlands Farmers' Association", 13.25, "tons" },
	{ "Highland Grayling", "Ouriad River Fisheries Cooperative", 7.80, "tons" },
	{ "Mountain Whitefish", "Ouriad River Fisheries Cooperative", 6.90, "tons" },
	{ "Brown Trout", "Ouriad River Fisheries Cooperative", 8.25, "tons" },
	{ "Lake Sturgeon Roe", "Ouriad River Fisheries Cooperative", 11.50, "kilograms" },
	{ "Whitefish (Spawning-Run)", "Ouriad River Fisheries Cooperative", 6.50, "tons" },
	{ "Lamprey", "Ouriad River Fisheries Cooperative", 9.75, "crates" },
	{ "Salted Lamprey Filets", "Ouriad River Fisheries Cooperative", 8.10, "tons" },
	{ "Smoked Burbot", "Ouriad River Fisheries Cooperative", 7.25, "tons" },
	{ "Steppe Horse (Working Breed)", "Caspazani Livestock Company", 3.2, "head" },
	{ "Highland Courier Mount", "Caspazani Livestock Company", 4.7, "head" },
	{ "Cavalry Stock (Auxiliary-Grade)", "Caspazani Livestock Company", 5.4, "head" },
	{ "Foal Training Licenses", "Caspazani Livestock Company", 0.4, "permits" },
	{ "Standard Saddle Kits (Hide & Wood)", "Caspazani Livestock Company", 1.6, "sets" },
	{ "Herding Bat Colony (Trained)", "Caspazani Livestock Company", 5.8, "colonies" },
	{ "Bat Guidance Whistle Sets", "Caspazani Livestock Company", 0.5, "kits" },
	{ "Pack Horse Leasing Contracts", "Caspazani Livestock Company", 1.9, "agreements" },
	{ "Horse Milk (Fermentable)", "Caspazani Livestock Company", 1.1, "liters" },
	{ "Livestock Registry Processing (Valley Standard)", "Caspazani Livestock Company", 0.3, "entries" },
	{ "White Beets", "White Ruby Communes", 7.124, "crates" },
	{ "Potatoes", "White Ruby Communes", 12.542, "sacks" },
	{ "Onions", "White Ruby Communes", 10.304, "sacks" },
	{ "Cabbage", "White Ruby Communes", 8.761, "heads" },
	{ "Garlic Bulbs", "White Ruby Communes", 1.984, "bundles" },
	{ "Dill", "White Ruby Communes", 0.673, "kilograms" },
	{ "Parsley", "White Ruby Communes", 0.544, "kilograms" },
	{ "Wild Juniper Berries", "White Ruby Communes", 0.298, "kilograms" },
	{ "Thyme Bush Cuttings", "White Ruby Communes", 0.186, "kilograms" },
	{ "Preserved Wild Boar (Spiced)", "White Ruby Communes", 0.462, "kilograms" },
	{ "Caledonian Spiced Ham", "White Ruby Communes", 0.117, "kilograms" },
	{ "Caledonian Cured Ham (Young)", "White Ruby Communes", 0.221, "kilograms" },
	{ "Purple Sturgeon Caviar", "White Ruby Communes", 0.037, "kilograms" },
	{ "Crab Roe Caviar", "White Ruby Communes", 0.052, "kilograms" },
	{ "Dried Pel'myeny Dumplings", "White Ruby Communes", 3.210, "packs" },
	{ "Borsch Base Mix (Dehydrated)", "White Ruby Communes", 2.901, "canisters" },
	{ "Velvet Antler Capsules", "Fange Velvet", 0.245, "bottles" },
	{ "Powdered Velvet Antler", "Fange Velvet", 1.102, "kilograms" },
	{ "Velvet Antler Slices (Dried)", "Fange Velvet", 0.538, "kilograms" },
	{ "Antler-Based Tonic Elixir", "Fange Velvet", 0.782, "liters" },
	{ "Velvet Antler Salve", "Fange Velvet", 0.312, "jars" },
	{ "Luxury Antler Infused Tea", "Fange Velvet", 0.874, "canisters" },
	{ "Antler Resin Bars", "Fange Velvet", 1.090, "bricks" },
	{ "Velvet Antler Wine (Fortified)", "Fange Velvet", 2.320, "bottles" },
	{ "Velvet Antler Soap", "Fange Velvet", 2.50, "bars" },
	{ "Antler Horn Carvings (Polished Ends)", "Fange Velvet", 0.152, "units" },
	{ "Wheat (Unmilled)", "Port of Corumia", 0.82, "tons" },
	{ "Oats (Unhulled)", "Port of Corumia", 0.52, "tons" },
	{ "Hay (Dry Bundled)", "Port of Corumia", 0.79, "bales" },
	{ "Cereal Base Grain Blend", "Port of Corumia", 0.87, "pallets" },
	{ "Nou-Oname Leaves (Dried)", "Port of Corumia", 12.00, "satchels" },
	{ "Adraso-Onam (Dried Herb Mix)", "Port of Corumia", 21.35, "bundles" },
	{ "Eeli-Illt-Eda Meat (Fresh)", "Port of Corumia", 5.5, "tons" },
	{ "Calamari (Lake Norton)", "Port of Corumia", 2.96, "tons" },
	{ "Jied-Rarev (Unrefined Pollen)", "Port of Corumia", 5.48, "kilograms" },
	{ "Fuy-Rabrev (Raw Resin)", "Port of Corumia", 5.96, "kilograms" },
	{ "Lowland Giraffe Meat (primal cuts)", "Lamian Ward", 6.80, "tons" },
	{ "Salted Giraffe Jerky", "Lamian Ward", 3.25, "slabs" },
	{ "Pemmican Cakes (tallow & dried meat)", "Lamian Ward", 2.10, "cakes" },
	{ "Rendered Tallow", "Lamian Ward", 2.95, "kilos" },
	{ "Marrow Oil", "Lamian Ward", 0.78, "barrels" },
	{ "Gelatin & Collagen Stock", "Lamian Ward", 1.35, "kilos" },
	{ "Bone-Broth Concentrate", "Lamian Ward", 4.20, "jars" },
	{ "Lowland Giraffe Hides (veg-tanned)", "Lamian Ward", 4.10, "sheets" },
	{ "Sinew/Tendon Cordage", "Lamian Ward", 0.95, "coils" },
	{ "Bone Flour (fertilizer)", "Lamian Ward", 2.40, "sacks" },
	{ "Bouldercast Alpine Fertilizer Pellets", "Sleva Farming Supply Cooperatives", 1.6, "tons" },
	{ "Bouldercast Terrace Blend (slow-release)", "Sleva Farming Supply Cooperatives", 1.25, "tons" },
	{ "Plug-Pit Seedling Mix (germination grade)", "Sleva Farming Supply Cooperatives", .875, "tons" },
	{ "Carapace Chitin-Calcium Flour (soil amendment)", "Sleva Farming Supply Cooperatives", .54, "tons" },
	{ "Molted Carapace Roofing Slabs (frost-proof)", "Sleva Farming Supply Cooperatives", 7.2, "slabs" },
	{ "Cuticle Grit (erosion & drainage aggregate)", "Sleva Farming Supply Cooperatives", .76, "tons" },
	{ "Root-Mat Foliar Extract (liquid feed)", "Sleva Farming Supply Cooperatives", 3.25, "barrels" },
	{ "Tri-Rostrum Microbial Inoculant", "Sleva Farming Supply Cooperatives", 45, "kilograms" },
	{ "Amber-Salt Deterrent Lures", "Sleva Farming Supply Cooperatives", 32, "packs" },
	{ "Terrace Stabilization Mat (biocomposite)", "Sleva Farming Supply Cooperatives", 18, "rolls" },
	{ "Woolhorse Underfleece (raw, Grade A)", "Hrimmanes Woolhorse Company", 2.4, "bales" },
	{ "Woolhorse Underfleece (raw, Grade B)", "Hrimmanes Woolhorse Company", 1.6, "bales" },
	{ "De-Guarded Woolhorse Tops", "Hrimmanes Woolhorse Company", 3.1, "bales" },
	{ "Woolhorse Spun Yarn (dk/worsted, 50 kg)", "Hrimmanes Woolhorse Company", 2.2, "crates" },
	{ "Woolhorse Felt Bolts (10 m x 1.4 m)", "Hrimmanes Woolhorse Company", 1.8, "bolts" },
	{ "Pack-Train Woolhorse (geldings)", "Hrimmanes Woolhorse Company", 0.85, "head" },
	{ "Breeding Woolhorse (mares)", "Hrimmanes Woolhorse Company", 1.10, "head" },
	{ "Cold-Weather Tack Set (saddle, collars, panniers)", "Hrimmanes Woolhorse Company", 7.50, "sets" },
	{ "Prime Reindeer Pelts", "King's Home Fur Trappers' Cooperative", 6.75, "bundles" },
	{ "Snow-Hare Fur (Winter White)", "King's Home Fur Trappers' Cooperative", 4.10, "bundles" },
	{ "Arctic Fox Pelts (Grade A)", "King's Home Fur Trappers' Cooperative", 7.90, "bundles" },
	{ "Tanned Wolf Hides", "King's Home Fur Trappers' Cooperative", 5.60, "sheets" },
	{ "Bear Fat Rendering (Cooking & Lamp Grade)", "King's Home Fur Trappers' Cooperative", 1.85, "barrels" },
	{ "Smoked Venison (Cold-Steppe Cure)", "King's Home Fur Trappers' Cooperative", 3.40, "tons" },
	{ "Antler Carving Blanks", "King's Home Fur Trappers' Cooperative", 0.88, "crates" },
	{ "Sinew Cordage (Hand-Twisted)", "King's Home Fur Trappers' Cooperative", 0.52, "coils" },
	{ "Trapper’s Tallow Soap (Pine & Resin Blend)", "King's Home Fur Trappers' Cooperative", 2.25, "bars" },
	{ "Frontier Survival Kits (Flint, Cord, Resin, Hide Pouch)", "King's Home Fur Trappers' Cooperative", 0.95, "sets" },
	{ "Caledonj Deep-Bay Lobster", "Caledonj Bay Fishermen’s’ Cooperatives", 1.875, "tons" },
	{ "Lobster Roe (Fresh)", "Caledonj Bay Fishermen’s’ Cooperatives", 6.80, "kilograms" },
	{ "Salt-Cured Lobster Meat", "Caledonj Bay Fishermen’s’ Cooperatives", 1.725, "tons" },
	{ "Pickled Lobster Claws (Spiced)", "Caledonj Bay Fishermen’s’ Cooperatives", 2.35, "jars" },
	{ "Lobster Bisque Base", "Caledonj Bay Fishermen’s’ Cooperatives", 3.90, "barrels" },
	{ "Smoked Lobster Fillets", "Caledonj Bay Fishermen’s’ Cooperatives", 1.850, "tons" },
	{ "Lobster Shell Powder (Culinary Use)", "Caledonj Bay Fishermen’s’ Cooperatives", 4.75, "kilograms" },
	{ "Chitin Extract (Cosmetic Grade)", "Caledonj Bay Fishermen’s’ Cooperatives", 7.80, "kilograms" },
	{ "Amber-Lobster Jerky", "Caledonj Bay Fishermen’s’ Cooperatives", 6.40, "slabs" },
	{ "Lobster Broth Concentrate", "Caledonj Bay Fishermen’s’ Cooperatives", 5.60, "bottles" },
	{ "Deep-Bay Lobster Oil", "Caledonj Bay Fishermen’s’ Cooperatives", 4.30, "liters" },
	{ "Crushed Lobster Shell Fertilizer", "Caledonj Bay Fishermen’s’ Cooperatives", 2.95, "sacks" },
	{ "Amber-Spiced Lobster Sausage", "Caledonj Bay Fishermen’s’ Cooperatives", 9.25, "tons" },
	{ "Marinated Lobster Medallions", "Caledonj Bay Fishermen’s’ Cooperatives", 3.25, "trays" },
	{ "Caledonj Lobster Liver Paste", "Caledonj Bay Fishermen’s’ Cooperatives", 2.10, "jars" },
}

---------------------------------------------------------
-- 3. Special Date Events
---------------------------------------------------------
local events = {
	["1,6"]  = "Bayram al-Nur (Festival of Light) [Vaeringheim]",
	["1,18"] = "Chag Or Hadash (Festival of New Light) [Luminaria]",
	["1,30"] = "Symposion Eirinis (Symposium of Harmony) [Serena]",
	["1,43"] = "Alev Günü (Day of Flame) [Pyralis]",
	["1,48"] = "Day of Abandonment [Vaeringheim]",
	["1,55"] = "Tikkun Tzel (Repair of Shadows) [Symphonara]",
	["2,6"]  = "Panegyris Chrysou (Golden Gathering) [Aurelia]",
	["2,17"] = "Constitution Day [Luminaria]",
	["2,19"] = "Mehtap Dalgası (Moonlit Tide) [Vaeringheim]",
	["2,31"] = "Oneiro Foteino (Dream of Illumination) [Somniumpolis]",
	["2,39"] = "Anniversary of victory in the New South Jangsong Campaign [Nexa]",
	["2,44"] = "Erev Galgal (Eve of Cycles) [Nexa]",
	["2,45"] = "Bassaridia Festival [Somniumpolis]",
	["2,48"] = "Anniversary of victory in the Southern Lake Morovia Campaign [Somniumpolis]",
	["2,54"] = "Leilat al-Kamar (Night of the Moon) [Lunalis Sancta]",
	["2,61"] = "Taşrakah (Reverence of the Stone) [Luminaria]",
	["3,6"]  = "Chag Tvuah (Festival of Harvest) [Sylvapolis]",
	["3,18"] = "Anagenesis Eirmos (Procession of Rebirth) [Acheron]",
	["3,28"] = "Panagia Therizis (Holy Day of the Reaper) [Sylvapolis]",
	["3,37"] = "Anniversary of victory in the Morovian Frontier Campaign [Acheron]",
	["3,43"] = "Karnavali Thysias (Carnival of Celebration) [Erythros]",
	["3,53"] = "Sefar Yashar (Straight Path Celebration) [Catonis Atrium]",
}

---------------------------------------------------------
-- 4. simulatePriceChange
--    Randomly modifies the base price daily,
--    applying a slight additional boost if
--    a special event occurs on that dateKey.
---------------------------------------------------------
local function simulatePriceChange(basePrice, dayOfMonth, month)
	local dateKey = string.format("%d,%d", month, dayOfMonth)
	local changeFactor = math.random(-20, 20) / 100
	local explanation

	if events[dateKey] then
		explanation = events[dateKey] .. ": Prices influenced by event dynamics."
		changeFactor = changeFactor + 0.05 -- small boost for special events
	else
		explanation = "General market fluctuations."
	end

	local newPrice = math.floor((basePrice * (1 + changeFactor)) * 100) / 100
	return newPrice, changeFactor, explanation
end

---------------------------------------------------------
-- 5. determinePriceLevel
--    Swapped so that:
--      adjustedPrice > basePrice ⇒ "Low"
--      adjustedPrice < basePrice ⇒ "High"
--      otherwise ⇒ "Standard"
---------------------------------------------------------
local function determinePriceLevel(basePrice, adjustedPrice)
	if adjustedPrice > basePrice then
		return "Low"
	elseif adjustedPrice < basePrice then
		return "High"
	else
		return "Standard"
	end
end

---------------------------------------------------------
-- Args helpers (for modes)
---------------------------------------------------------
local function getArgs(frame)
	if type(frame) ~= "table" then
		return {}
	end

	local args = {}
	local fargs = frame.args or {}

	local pargs = {}
	if frame.getParent then
		local parent = frame:getParent()
		if parent and parent.args then
			pargs = parent.args
		end
	end

	for k, v in pairs(pargs) do
		if v ~= nil and v ~= "" then
			args[k] = v
		end
	end
	for k, v in pairs(fargs) do
		if v ~= nil and v ~= "" then
			args[k] = v
		end
	end

	return args
end

local function isNo(v)
	v = tostring(v or ""):lower()
	return (v == "no" or v == "n" or v == "false" or v == "0")
end

local function levelSymbol(priceLevel)
	if priceLevel == "Low" then
		return "↑"
	elseif priceLevel == "High" then
		return "↓"
	else
		return "•"
	end
end

---------------------------------------------------------
-- 6. MAIN Table Generation (Condensed + Optional Full Breakdown)
---------------------------------------------------------
function p.generateTable(frame)
	local args = getArgs(frame)
	local mode = tostring(args.mode or "both"):lower()  -- both | summary | breakdown
	local collapse = not isNo(args.collapse)            -- default true (collapsed)

	local showSummary = (mode == "both" or mode == "summary")
	local showBreakdown = (mode == "both" or mode == "breakdown")

	-- Step A: daily seed for randomness (unique per day)
	local localTime = os.date("*t")
	local dailySeed = localTime.year * 1000 + localTime.yday
	math.randomseed(dailySeed)

	-- Step B: date info
	local dateString, dayOfMonth, month = getCurrentDate()

	-- Step C: build outputs
	local out = ""

	local summary = ""
	if showSummary then
		summary =
			"{| class=\"wikitable sortable\"\n" ..
			"! Date (Day/Month/Year [[PSSC]]) !! Δ !! Product !! Producing Company !! Tier I Price !! Units !! Notes\n"
	end

	local breakdown = ""
	if showBreakdown then
		local collapseClass = collapse and " mw-collapsible mw-collapsed" or ""
		breakdown =
			"{| class=\"wikitable sortable" .. collapseClass .. "\"\n" ..
			"|+ Full tier breakdown" .. (collapse and " (click to expand)" or "") .. "\n" ..
			"! Date (Day/Month/Year [[PSSC]]) !! Product !! Producing Company !! Base Price !! Price Level !! Tier III Price !! Tier II Price !! Tier I Price !! Units !! Explanation\n"
	end

	-- track averages
	local totalBase, totalNew, count = 0, 0, 0

	-- Step D: rows
	for _, product in ipairs(products) do
		local name = product[1]
		local company = product[2]
		local basePrice = product[3]
		local unit = product[4]

		local newPrice, changeFactor, explanation = simulatePriceChange(basePrice, dayOfMonth, month)
		local priceLevel = determinePriceLevel(basePrice, newPrice)
		local sym = levelSymbol(priceLevel)

		local rowStyle
		if priceLevel == "Low" then
			rowStyle = ' style="background-color:lightblue;"'
		elseif priceLevel == "Standard" then
			rowStyle = ' style="background-color:lightgreen;"'
		else
			rowStyle = ' style="background-color:#ffcccb;"'
		end

		local tierIIIPrice = math.floor((newPrice / 3) * 100) / 100
		local tierIIPrice  = math.floor((newPrice * 2 / 3) * 100) / 100
		local tierIPrice   = newPrice

		totalBase = totalBase + basePrice
		totalNew  = totalNew + newPrice
		count = count + 1

		if showSummary then
			summary = summary .. string.format(
				"|-%s\n| %s || %s || %s || %s || %.2f || %s || %s\n",
				rowStyle, dateString, sym, name, company, tierIPrice, unit, explanation
			)
		end

		if showBreakdown then
			breakdown = breakdown .. string.format(
				"|-%s\n| %s || %s || %s || %.2f || %s || %.2f || %.2f || %.2f || %s || %s\n",
				rowStyle, dateString, name, company, basePrice, priceLevel,
				tierIIIPrice, tierIIPrice, tierIPrice, unit, explanation
			)
		end
	end

	-- Step E: averages
	local avgBase = totalBase / count
	local avgNew = totalNew / count

	local averagePriceLevel
	if avgNew > avgBase then
		averagePriceLevel = "Low"
	elseif avgNew < avgBase then
		averagePriceLevel = "High"
	else
		averagePriceLevel = "Standard"
	end
	local avgSym = levelSymbol(averagePriceLevel)

	if showSummary then
		summary = summary .. string.format(
			"|-\n! colspan=\"7\" | Average adjusted (%.2f) vs base (%.2f): %s %s\n|}\n",
			avgNew, avgBase, avgSym, averagePriceLevel
		)
		out = out .. summary .. "\n"
	end

	if showBreakdown then
		breakdown = breakdown .. string.format(
			"|-\n! colspan=\"10\" | Average adjusted (%.2f) vs base (%.2f): %s %s\n|}\n",
			avgNew, avgBase, avgSym, averagePriceLevel
		)
		out = out .. breakdown
	end

	return out
end

---------------------------------------------------------
-- Convenience: full expanded breakdown (for updating/audits)
---------------------------------------------------------
function p.full(frame)
	frame = frame or {}
	frame.args = frame.args or {}
	frame.args.mode = "breakdown"
	frame.args.collapse = "no"
	return p.generateTable(frame)
end

return p