local ReplicatedStorage = game:GetService("ReplicatedStorage")

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

local TradeService = {}
TradeService.__index = TradeService

function TradeService:SellAnimalToNpc(state, x, y)
	local key = GridUtils.CellKey(x, y)
	local cell = state.grid.cells[key]
	if cell == nil or cell.kind ~= "animal" then
		return false, "NoAnimal"
	end

	local animalDef = LootTables.Animals[cell.id]
	if animalDef == nil then
		error("TradeService: unknown animal")
	end

	local mutationMultiplier = MutationRules:GetIncomeMultiplier(cell.mutation)
	local sellValue = Economy.GetSellValue(
		animalDef.baseIncomePerSecond,
		cell.level,
		mutationMultiplier,
		animalDef.sellFactor
	)

	state.grid.cells[key] = nil
	state.coins += sellValue
	return true, sellValue
end

return TradeService
