local ReplicatedStorage = game:GetService("ReplicatedStorage")

local LootTables = require(ReplicatedStorage.Shared.LootTables)
local MutationRules = require(ReplicatedStorage.Shared.MutationRules)
local Economy = require(ReplicatedStorage.Shared.Economy)

local IncomeService = {}
IncomeService.__index = IncomeService

local function incomePerSecond(cell)
	local animalDef = LootTables.Animals[cell.id]
	if animalDef == nil then
		error("IncomeService: unknown animal in grid")
	end

	local mutationMult = MutationRules:GetIncomeMultiplier(cell.mutation)
	local levelMult = Economy.GetLevelMultiplier(cell.level)
	return animalDef.baseIncomePerSecond * mutationMult * levelMult
end

function IncomeService:Tick(state, dt, eventModifiers)
	local total = 0
	for _, cell in pairs(state.grid.cells) do
		if cell.kind == "animal" then
			total += incomePerSecond(cell) * dt * eventModifiers.incomeMultiplier
		end
	end

	state.coins += total
	return total
end

return IncomeService
