local PLUGIN = PLUGIN

util.AddNetworkString("ixKitMenu")
util.AddNetworkString("ixKitSync")
util.AddNetworkString("ixKitClaim")

function PLUGIN:SaveData()
	self:SetData(self.kits)
end

function PLUGIN:LoadData()
	self.kits = self:GetData() or {}
end

function PLUGIN:SyncKits(client)
	net.Start("ixKitSync")
		net.WriteTable(self.kits)
	if (client) then
		net.Send(client)
	else
		net.Broadcast()
	end
end

function PLUGIN:PlayerLoadedCharacter(client, character)
	self:SyncKits(client)
end

net.Receive("ixKitClaim", function(len, client)
	local character = client:GetCharacter()
	if (!character) then return end

	local weaponClass = net.ReadString()
	local kit = PLUGIN.kits[weaponClass]

	if (!kit) then return end

	-- Проверка группы
	local allowedGroups = {
		["superadmin"] = true,
		["sponsor"] = true,
		["prem"] = true
	}

	if (!allowedGroups[client:GetUserGroup()]) then
		client:Notify("У вас нет доступа.")
		return
	end

	-- Проверка КД на персонаже
	local cooldowns = character:GetData("kit_cooldowns", {})
	local lastClaim = cooldowns[weaponClass] or 0
	local remaining = (lastClaim + kit.cooldown) - os.time()

	if (remaining > 0) then
		client:Notify("Откат еще " .. math.ceil(remaining / 60) .. " мин.")
		return
	end

	-- Выдача
	client:Give(weaponClass)
	client:SelectWeapon(weaponClass)
	
	-- Установка КД
	cooldowns[weaponClass] = os.time()
	character:SetData("kit_cooldowns", cooldowns)

	client:Notify("Вы получили кит: " .. kit.name)
end)
