local QBCore = exports['qb-core']:GetCoreObject()

local function IsJobAllowed(jobName)
    if Config.RequiredJobs and type(Config.RequiredJobs) == 'table' then
        for _, allowedJob in ipairs(Config.RequiredJobs) do
            if jobName == allowedJob then
                return true
            end
        end
        return false
    end
    return false
end

local function GetRandomName()
    local first = Config.RandomNames.firstNames[math.random(#Config.RandomNames.firstNames)]
    local last = Config.RandomNames.lastNames[math.random(#Config.RandomNames.lastNames)]
    return first .. ' ' .. last
end

RegisterNetEvent('paradsie_checkvin:checkVehicleInfo')
AddEventHandler('paradsie_checkvin:checkVehicleInfo', function(plate, model)
    local src = source
    local Player = QBCore.Functions.GetPlayer(src)
    
    if not IsJobAllowed(Player.PlayerData.job.name) then
        return
    end
    
    MySQL.Async.fetchAll('SELECT * FROM player_vehicles WHERE plate = @plate', {
        ['@plate'] = plate
    }, function(result)
        local vehicleInfo = {
            plate = plate,
            model = model,
            owner = "Unknown",
            status = "UNREGISTERED",
            insurance = "NONE",
            registration = "EXPIRED",
            vinscratch = nil
        }
        
        if result[1] then
            MySQL.Async.fetchAll('SELECT * FROM players WHERE citizenid = @citizenid', {
                ['@citizenid'] = result[1].citizenid
            }, function(playerResult)
                if playerResult[1] then
                    local charinfo = json.decode(playerResult[1].charinfo)
                    vehicleInfo.owner = charinfo.firstname .. ' ' .. charinfo.lastname
                    vehicleInfo.status = "REGISTERED"
                    vehicleInfo.insurance = "VALID"
                    vehicleInfo.registration = "VALID"
                end
                -- Optional: Rahe Boosting VIN scratch integration
                if Config.RaheBoosting and Config.RaheBoostingResource then
                    local ok, isScratched = pcall(function()
                        local exp = exports[Config.RaheBoostingResource]
                        if exp and exp.IsVinScratched then
                            return exp.IsVinScratched(plate)
                        end
                        if exp and exp.isVinScratched then
                            return exp.isVinScratched(plate)
                        end
                        return nil
                    end)
                    if ok then vehicleInfo.vinscratch = isScratched end
                end

                TriggerClientEvent('paradsie_checkvin:receiveVehicleInfo', src, vehicleInfo)
            end)
        else
            vehicleInfo.owner = GetRandomName()
            if Config.RaheBoosting and Config.RaheBoostingResource then
                local ok, isScratched = pcall(function()
                    local exp = exports[Config.RaheBoostingResource]
                    if exp and exp.IsVinScratched then
                        return exp.IsVinScratched(plate)
                    end
                    if exp and exp.isVinScratched then
                        return exp.isVinScratched(plate)
                    end
                    return nil
                end)
                if ok then vehicleInfo.vinscratch = isScratched end
            end
            TriggerClientEvent('paradsie_checkvin:receiveVehicleInfo', src, vehicleInfo)
        end
    end)
end)
