local md5 = require "resty.md5"
local str = require "resty.string"
local mlcache = require "sm.utils.mlcache"
local error = require "sm.utils.error"
local redis = require "sm.utils.redis"
local global_config = require "sm.config"
local cjson = require "cjson"

local M = {}

-- Grab hostname config
function M.hostname(redis, hostnameStr)

    local hostname, hit_level, err = mlcache.get(hostnameStr, redis.hgetall, hostnameStr)
    if not hostname then
        return nil, nil, err
    end

    return hostname, hit_level, err
end

-- Grab zone config
function M.zone(redis, key)
    -- Grab config from mlcache
    local config, hit_level, err = mlcache.get(key, redis.hgetall, key)
    --if err then
    --    return nil, nil, err
    --end

    return config, hit_level, err
end

function M.getDomainConf(redis, key)
    -- Grab config from mlcache
    local config, hit_level, err = mlcache.get(key, redis.get, key)
    --if err then
    --    return nil, nil, err
    --end

    return config, hit_level, err
end

-- Grab ssl config
function M.ssl(redis, zone)

    local certificate_key = zone.. ":certificate"
    local private_key_key = zone.. ":private_key"

    -- Grab config from mlcache
    local certificate, hit_level, err = mlcache.get(certificate_key, redis.get, certificate_key)
    
    if err then
       return nil, nil, err
    end

    local private_key, hit_level, err = mlcache.get(private_key_key, redis.get, private_key_key)

    return certificate, private_key, hit_level, err
end

return M