local PLUGIN = PLUGIN
PLUGIN.name = "Warn System"
PLUGIN.author = "Scripty"
PLUGIN.description = "Modern Warning System for administrators with automatic demotion."

print("[Warn System] Loading Plugin...")

ix.util.Include("sv_plugin.lua")
ix.util.Include("cl_plugin.lua")
ix.util.Include("cl_derma.lua")

if (SERVER) then
    util.AddNetworkString("ixWarnSync")
    util.AddNetworkString("ixWarnAdd")
    util.AddNetworkString("ixWarnRemove")
    util.AddNetworkString("ixWarnMenu")
end

ix.command.Add("warn", {
    description = "Open the warning management menu.",
    adminOnly = true,
    superAdminOnly = true, -- Helix support varies, so we keep CanRun
    CanRun = function(self, client)
        return client:IsSuperAdmin()
    end,
    OnRun = function(self, client)
        if (!client:IsSuperAdmin()) then return end

        local admins = {}
        local warns = ix.data.Get("warns", {})

        -- Get all players who are online and admins
        for _, v in ipairs(player.GetAll()) do
            if (v:IsAdmin()) then
                admins[v:SteamID()] = {
                    name = v:Name(),
                    steamID = v:SteamID(),
                    rank = v:GetUserGroup(),
                    online = true
                }
            end
        end

        -- Add offline admins who have warnings (Helix data)
        for steamID, data in pairs(warns) do
            if (!admins[steamID]) then
                admins[steamID] = {
                    name = data.name or "Unknown",
                    steamID = steamID,
                    rank = data.rank or "N/A",
                    online = false
                }
            end
        end

        -- Try to fetch ALL admins from SAM database if available
        if (SERVER and sam and sam.SQL) then
            sam.SQL.FQuery("SELECT `steamid`, `name`, `rank` FROM `sam_players` WHERE `rank` != 'user'", {}, function(data)
                if (data) then
                    for _, row in ipairs(data) do
                        local steamID = row.steamid
                        -- Use SteamID if it's already a string, or format it
                        if (!admins[steamID]) then
                            admins[steamID] = {
                                name = (row.name != "" and row.name) or "Unknown",
                                steamID = steamID,
                                rank = row.rank or "N/A",
                                online = false
                            }
                        end
                    end
                end

                if (IsValid(client)) then
                    net.Start("ixWarnMenu")
                        net.WriteTable(admins)
                        net.WriteTable(warns)
                    net.Send(client)
                end
            end)
        else
            -- Fallback/Helix only
            net.Start("ixWarnMenu")
                net.WriteTable(admins)
                net.WriteTable(warns)
            net.Send(client)
        end
    end
})
