--[[
How it'll work

client sends "Place" req
server gets "Place" req
server checks spot to place, overlapping note, ratelimiting of notes, etc

if success then
	server sends back "Good" to client
else
	server sends back "Bad" to client
end

]]

local _RunServ = game:GetService("RunService")
local _Players = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local PlacedNotes = workspace.PlacedNotes
local BaseNoteGhost = workspace.BaseNote:Clone()

local SharedBridges = require(Replicated.SharedBridges)

local Bridges = SharedBridges.Bridges
local Identifiers = SharedBridges.Identifiers

local PlaceBridge = Bridges.PlaceNote

local ServerParams = OverlapParams.new()
ServerParams.FilterType = Enum.RaycastFilterType.Whitelist
ServerParams.FilterDescendantsInstances = { PlacedNotes, workspace.Baseplate }

local noteCount = 0

local function CheckOverLap(Note: Part, CF: CFrame)
	local overlaps = workspace:GetPartBoundsInBox(CF, Note.Size, ServerParams)
	return #overlaps > 0 and true or false
end

local function OnPlace(Player: Player, CF: CFrame)
	local Note = BaseNoteGhost:Clone()
	if CheckOverLap(Note, CF) then
		PlaceBridge:FireTo(Player, Identifiers.Bad)
		Note:Destroy()
		return
	end
	noteCount += 1
	Note.CFrame = CF
	Note.Name = tostring(noteCount)
	Note.Parent = PlacedNotes
	PlaceBridge:FireTo(Player, Identifiers.Good)
end

PlaceBridge:Connect(OnPlace)
