module Devops
  module API3
    module Routes
      module ScriptRoutes

        def self.registered(app)

          app.define_policy :read_scripts, "Get scripts list or script details"
          app.define_policy :add_scripts, "Add new scripts"
          app.define_policy :delete_scripts, "Delete scripts"
          app.define_policy :execute_scripts, "Execute scripts"

          # Get scripts names
          #
          # * *Request*
          #   - method : GET
          #   - headers :
          #     - Accept: application/json
          #
          # * *Returns* :
          #   [
          #     "script_1"
          #   ]
          app.get_with_headers "/scripts" do
            check_policy(:read_scripts)
            json Devops::API3::Handler::Script.new(request).scripts
          end

          # Run command on node :node_name
          #
          # * *Request*
          #   - method : POST
          #   - body :
          #     command to run
          #
          # * *Returns* : text stream
          app.post_with_statistic "/script/command/:server_id" do |server_id|
            check_policy(:execute_scripts)
            stream() do |out|
              begin
                Devops::API3::Handler::Script.new(request).execute_command(out, server_id)
              rescue IOError => e
                logger.error e.message
              end
            end
          end

          # Run script :script_name on nodes
          #
          # * *Request*
          #   - method : POST
          #   - headers :
          #     - Content-Type: application/json
          #   - body :
          #     {
          #       "nodes": [], -> array of nodes names
          #       "params": [] -> array of script arguments
          #     }
          #
          # * *Returns* : text stream
          app.post_with_headers "/script/run/:script_name" do |script_name|
            check_policy(:execute_scripts)
            stream() do |out|
              begin
                status = Devops::API3::Handler::Script.new(request).run_script out, script_name
                out << create_status(status)
              rescue IOError => e
                logger.error e.message
              end
            end
          end

          hash = {}
          # Create script :script_name
          #
          # * *Request*
          #   - method : PUT
          #   - headers :
          #     - Accept: application/json
          #   - body : script content
          #
          # * *Returns* :
          #   201 - Created
          hash["PUT"] = lambda { |script_name|
            check_policy(:add_scripts)
            Devops::API3::Handler::Script.new(request).create_script(script_name)
            create_response("File '#{script_name}' created", nil, 201)
          }

          # Delete script :script_name
          #
          # * *Request*
          #   - method : Delete
          #   - headers :
          #     - Accept: application/json
          #
          # * *Returns* :
          #   200 - Deleted
          hash["DELETE"] = lambda { |script_name|
            check_policy(:delete_scripts)
            Devops::API3::Handler::Script.new(request).delete_script script_name
            create_response("File '#{script_name}' deleted")
          }
          app.multi_routes "/script/:script_name", hash

          puts "Script routes initialized"
        end

      end
    end
  end
end
