module Devops
  module API3
    module Routes
      module JobTaskRoutes

        def self.registered(app)
          app.define_policy :read_tasks, "Read tasks"

          app.get_with_headers "/tasks/all" do
            check_policy(:read_tasks)
            json Devops::API3::Handler::JobTask.new(request).all.map{|r| r.to_hash}
          end

          app.get_with_headers "/tasks/all/latest" do
            check_policy(:read_tasks)
            json Devops::API3::Handler::JobTask.new(request).all_latest.map{|r| r.to_hash}
          end

          app.get_with_headers "/tasks/all/attributes/:name" do |name|
            check_policy(:read_tasks)
            json Devops::API3::Handler::JobTask.new(request).attributes(name)
          end

          app.get "/task/:id" do |id|
            #check_policy(:read_tasks)
            json Devops::API3::Handler::JobTask.new(request).task(id).to_hash
          end

          app.get "/task/:id/report" do |id|
            #check_policy(:read_tasks)
            @text, @done = Devops::API3::Handler::JobTask.new(request).report(id)
            erb :report
          end

          app.get "/task/:id/status" do |id|
            #check_policy(:read_tasks)
            r = Devops::API3::Handler::JobTask.new(request).status(id)
            return [404, "Job with id '#{id}' not found"] if r.nil?
            r
          end

          puts "JobTask routes initialized"
        end

      end
    end
  end
end

