local kong_meta = require "kong.meta"

local ZeroVersionRewriteHandler = {PRIORITY = 1201, VERSION = kong_meta.version}

function ZeroVersionRewriteHandler:access(conf)
    local path = kong.request.get_path()
    local new_path, n = ngx.re.sub(path, "^/v1alpha1/instances", "/zero/v1beta1/instances", "jo")
    if n and n > 0 then
        kong.service.request.set_path(new_path)
        kong.ctx.plugin.is_v1alpha1 = true
    end
end

function ZeroVersionRewriteHandler:response(conf)
    if kong.ctx.plugin.is_v1alpha1 then
        kong.response.set_header("Deprecation", "true")
        kong.response.set_header("Sunset", "Thu, 01 May 2026 00:00:00 GMT")
        kong.response.set_header("Link", '</v1beta1/instances>; rel="successor-version"')
    end
end

return ZeroVersionRewriteHandler
