local ReplicatedStorage = game:GetService("ReplicatedStorage")

local GridUtils = require(ReplicatedStorage.Shared.GridUtils)

local PlacementService = {}
PlacementService.__index = PlacementService

function PlacementService:CanPlace(state, x, y)
	if GridUtils.WithinBounds(state.grid.width, state.grid.height, x, y) == false then
		return false, "OutOfBounds"
	end
	local key = GridUtils.CellKey(x, y)
	if state.grid.cells[key] ~= nil then
		return false, "Occupied"
	end
	return true, nil
end

function PlacementService:PlaceEgg(state, eggType, tokenId, x, y, hatchAt, seed)
	local canPlace, reason = self:CanPlace(state, x, y)
	if canPlace == false then
		return false, reason
	end

	local key = GridUtils.CellKey(x, y)
	state.grid.cells[key] = {
		kind = "egg",
		eggType = eggType,
		tokenId = tokenId,
		hatchAt = hatchAt,
		seed = seed,
	}
	return true, nil
end

return PlacementService
