local _0xA=GetCurrentResourceName
local _0xB=GetResourceMetadata
local _0xC=PerformHttpRequest
local _0xD=print

local _0x1=_0xA()
local _0x2="https://update-api.santosmods.dev/SandyShoresReborn/latest-version"

local function _0x3()
    return _0xB(_0x1,"version",0) or "unknown"
end

local function _0x4(a)
    return
        a:match("Version:%s*(.-)\n"),
        a:match("Update Note:%s*(.-)\n"),
        a:match("Urgent:%s*(.-)\n"),
        a:match("Store:%s*(.-)\n"),
        a:match("LSPDFR:%s*(.-)$")
end

local function _0x5(v)
    local t={}
    for p in tostring(v):gmatch("[^%.]+") do
        t[#t+1]=tonumber(p) or 0
    end
    return t
end

local function _0x6(a,b)
    local x,y=_0x5(a),_0x5(b)
    for i=1,math.max(#x,#y) do
        local m,n=x[i] or 0,y[i] or 0
        if m>n then return 1 end
        if m<n then return -1 end
    end
    return 0
end

local function _0x7()
    _0xD("^1──────────────────────────────────────────────^7")
    _0xD("^6SandyShoresReborn^7  |  ^3Version Check^7")
    _0xD("^1──────────────────────────────────────────────^7")
end

local function _0x8()
    local v=_0x3()

    _0xC(_0x2,function(c,b)
        _0x7()

        if c~=200 or not b then
            _0xD("^1Update server unreachable.^7")
            _0xD("^3Installed:^7 "..v)
            return
        end

        local lv,n,u,s,l=_0x4(b)
        if not lv then
            _0xD("^1Invalid update data received.^7")
            return
        end

        _0xD("^3Installed:^7 "..v)
        _0xD("^3Official:^7  "..lv)
        _0xD("^1──────────────────────────────────────────────^7")

        local d=_0x6(v,lv)

        if d<0 then
            if u=="Yes" then
                _0xD("^1URGENT UPDATE REQUIRED^7")
            else
                _0xD("^6Update available^7")
            end
            if n then _0xD("^7"..n) end
            if s then _0xD("^5Store:^7 "..s) end
            if l then _0xD("^5LSPDFR:^7 "..l) end

        elseif d>0 then
            _0xD("^1WARNING: UNOFFICIAL VERSION DETECTED^7")
            _0xD("^1This may not be the official version of this asset.^7")
            if s then _0xD("^5Official Store:^7 "..s) end
            if l then _0xD("^5Official LSPDFR:^7 "..l) end

        else
            _0xD("^2Resource verified and up to date.^7")
        end

        _0xD("^1──────────────────────────────────────────────^7")
    end,"GET")
end

AddEventHandler("onResourceStart",function(r)
    if r~=_0x1 then return end
    CreateThread(function()
        Wait(15000)
        _0x8()
    end)
end)

RegisterCommand("sandycheck",function(s)
    if s~=0 then return end
    _0x8()
end,true)
