RegisterCommand('inv', function(source, args)
	if source ~= 0 then
		local _source = source
        local user = exports.spyoo_base:getUser(_source)
        if user.permission >= 5 then
            local command = args[1]
            if command == "give" then
                give(_source, args)
            end
            if command == "clear" then
                clear(_source, args)
            end
            if command == "save" then
                if user.permission >= 10 then
                    saveCommand(_source, args)
                else
                    TriggerClientEvent("spyoo_notif:notif", _source, { notif = "Vous n'avez pas la permission pour faire cela (niveau de permission demandé : 10)", type = "error"})
                end
            end
        else 
            TriggerClientEvent("spyoo_notif:notif", _source, { notif = "Vous n'avez pas la permission pour faire cela (niveau de permission demandé : 5)", type = "error"})
        end
	end
end)

function give(_source, args)
    local to = args[2]
    local item = args[3]
    local quantity = tonumber(args[4])
    local data = json.decode(args[5])

    local receiver = to
    if receiver == "me" then
        receiver = _source
    end

    if not exports.spyoo_base:isPlayerOnline(receiver) then
        TriggerClientEvent("spyoo_notif:notif", _source, { notif = "Aucun joueur avec l'ID " .. to .. " trouvé", type = "error"})
        return
    end

    if item ~= nil and quantity ~= nil then
        if Config.Items[item] then
            MySQL.Sync.execute("INSERT INTO log_give (giver, receiver, item, quantity, data) VALUES (@giver, @receiver, @item, @quantity, @data)", {
                ['@giver'] = exports.spyoo_base:getSteamId(_source),
                ['@receiver'] = exports.spyoo_base:getSteamId(receiver),
                ['@item'] = item,
                ['@quantity'] = quantity,
                ['@data'] = json.encode(data)
            })
            TriggerEvent("spyoo_inventory:addItem", receiver, item, quantity, data, true)
            if to ~= "me" and to ~= _source then
                TriggerClientEvent("spyoo_notif:notif", _source, { notif = "Vous avez donné : " .. Config.Items[item].label .. " x " .. quantity .. " à " .. GetPlayerName(send), type = "icone", content = "fa-solid fa-circle-exclamation"})
            end
        else 
            TriggerClientEvent("spyoo_notif:notif", _source, { notif = "L'item : " .. item .. " n'existe pas", type = "error"})
        end
    else
        TriggerClientEvent("spyoo_notif:notif", _source, { notif = 'ERREUR Commande "give" utilisation : /inv give id (ou me pour vous la donner à vous même) item quantity data (data peut être vide)', time = 8000, type = "error"})
    end
end

function clear(_source, args)
    local to = args[2]
    if to == "me" or to == nil then
        to = _source
    end
    if not exports.spyoo_base:isPlayerOnline(to) then
        TriggerClientEvent("spyoo_notif:notif", _source, { notif = "Aucun joueur avec l'ID " .. to .. " trouvé", type = "error"})
        return
    end
    local identifier = exports.spyoo_base:getSteamId(to)
    Inventory[identifier] = {}
    if to ~= "me" and to ~= _source then
        TriggerClientEvent("spyoo_notif:notif", _source, { notif = "Vous avez remis l'inventaire de " .. GetPlayerName(to) .. " à 0"})
    end
    TriggerClientEvent("spyoo_notif:notif", to, { notif = "Votre inventaire a été remis à 0 par " .. GetPlayerName(_source), type = "icone", content = "fa-solid fa-circle-exclamation"})
    MySQL.Sync.execute("DELETE FROM inventory WHERE identifier = @identifier", {
        ['@identifier'] = identifier
    })
    MySQL.Sync.execute("INSERT INTO log_clear (admin, player) VALUES (@admin, @player)", {
        ['@admin'] = exports.spyoo_base:getSteamId(_source),
        ['@player'] = identifier,
    })
end

function saveCommand(_source, args)
    local to = args[2]
    if to == "me" or to == nil then
        to = _source
    end
    if not exports.spyoo_base:isPlayerOnline(to) then
        TriggerClientEvent("spyoo_notif:notif", _source, { notif = "Aucun joueur avec l'ID " .. to .. " trouvé", type = "icone", content = "fa-solid fa-circle-exclamation"})
        return
    end
    local identifier = exports.spyoo_base:getSteamId(to)
    saveInventoryPlayer(identifier)
    TriggerClientEvent("spyoo_notif:notif", to, { notif = "Votre inventaire a été sauvegarder par " .. GetPlayerName(_source), type = "icone", content = "fa-solid fa-circle-exclamation"})
end