require 'swagger/blocks'

require_relative 'devops_error'
require_relative 'devops_response'

module Devops
  module API3
    module Docs
      class ServerRoutes

        include Swagger::Blocks

        swagger_schema :ServerIdObject do
          property :id do
            key :type, :string
          end
        end

        swagger_schema :DevopsServerResponse do
          allOf do
            schema do
              key :'$ref', :DevopsResponse
            end
            schema do
              property :id do
                key :type, :string
              end
            end
          end
        end

        swagger_path "/servers" do
          operation :get do
            key :description, 'Get servers list'
            key :operationId, 'getServers'
            key :tags, [
              'server'
            ]
            response 200 do
              key :description, 'Servers list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :ServerObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/servers/provider/{provider}/{account}" do
          operation :get do
            key :description, 'Get servers list by provider and provider account'
            key :operationId, 'getServersByProviderAccount'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :provider
              key :in, :path
              key :description, 'Provider name'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :account
              key :in, :path
              key :description, 'Provider account id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Servers list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :ServerObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/id/{name}" do
          operation :get do
            key :description, 'Get server id by name'
            key :operationId, 'getServerIdByName'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :name
              key :in, :path
              key :description, 'Server name'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server id'
              schema do
                key :'$ref', :ServerIdObject
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}" do
          operation :get do
            key :description, 'Get server by id'
            key :operationId, 'getServerById'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server'
              schema do
                key :'$ref', :ServerObject
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}" do
          operation :delete do
            key :description, 'Delete server by id'
            key :operationId, 'deleteServerById'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'List of jobs ids'
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/servers/delete" do
          operation :post do
            key :description, 'Delete servers'
            key :operationId, 'deleteServers'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :server_ids
              key :in, :body
              key :description, 'Array of servers ids'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'List of jobs ids'
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/servers/{id}/deploy" do
          operation :post do
            key :description, 'Deploy server'
            key :operationId, 'deployServer'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :tags
              key :in, :body
              key :description, 'Chef tags to deploy with'
              key :required, false
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            parameter do
              key :name, :run_list
              key :in, :body
              key :description, 'Chef run_list to deploy with'
              key :required, false
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            parameter do
              key :name, :build_number
              key :in, :body
              key :description, 'Build number to deploy'
              key :required, false
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'List of jobs ids'
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server" do
          operation :post do
            key :description, 'Create server'
            key :operationId, 'createServer'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :project
              key :in, :body
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :environment
              key :in, :body
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :category
              key :in, :body
              key :description, 'Category id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :name
              key :in, :body
              key :description, 'Server name'
              key :required, false
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :cm_name
              key :in, :body
              key :description, 'Server name in configuration manager system'
              key :required, false
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :without_bootstrap
              key :in, :body
              key :description, 'Skip bootstrap phase'
              key :required, false
              schema do
                key :type, :bool
              end
            end
            parameter do
              key :name, :skip_rollback
              key :in, :body
              key :description, 'Skip rollback on error'
              key :required, false
              schema do
                key :type, :bool
              end
            end
            parameter do
              key :name, :run_list
              key :in, :body
              key :description, 'Chef run_list to deploy with'
              key :required, false
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'List of jobs ids'
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}/pause" do
          operation :post do
            key :description, 'Pause server'
            key :operationId, 'pauseServer'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response 409 do
              key :description, 'Conflict error, server in invalid state, operation can not be done'
              schema do
                key :'$ref', :DevopsError
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}/unpause" do
          operation :post do
            key :description, 'Unpause server'
            key :operationId, 'unpauseServer'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response 409 do
              key :description, 'Conflict error, server in invalid state, operation can not be done'
              schema do
                key :'$ref', :DevopsError
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}/reserve" do
          operation :post do
            key :description, 'Reserve server'
            key :operationId, 'reserveServer'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response 409 do
              key :description, 'Conflict error, server in invalid state, operation can not be done'
              schema do
                key :'$ref', :DevopsError
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}/unreserve" do
          operation :post do
            key :description, 'Unreserve server'
            key :operationId, 'unreserveServer'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response 409 do
              key :description, 'Conflict error, server in invalid state, operation can not be done'
              schema do
                key :'$ref', :DevopsError
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}/bootstrap" do
          operation :post do
            key :description, 'Bootstrap server'
            key :operationId, 'bootstrapServer'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :run_list
              key :in, :body
              key :description, 'Chef run_list to deploy with'
              key :required, false
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            parameter do
              key :name, :bootstrap_template
              key :in, :body
              key :description, 'Owerride bootstrap_template parameter'
              key :required, false
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :cm_name
              key :in, :body
              key :description, 'Server name in configuration manager system'
              key :required, false
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response 409 do
              key :description, 'Conflict error, server in invalid state, operation can not be done'
              schema do
                key :'$ref', :DevopsError
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}/unbootstrap" do
          operation :post do
            key :description, 'Unbootstrap server'
            key :operationId, 'unbootstrapServer'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response 409 do
              key :description, 'Conflict error, server in invalid state, operation can not be done'
              schema do
                key :'$ref', :DevopsError
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/add" do
          operation :post do
            key :description, 'Add static server'
            key :operationId, 'addServer'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :project
              key :in, :body
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :environment
              key :in, :body
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :category
              key :in, :body
              key :description, 'Category id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :private_ip
              key :in, :body
              key :description, 'Server private ip to connect to'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :key
              key :in, :body
              key :description, 'Key id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :remote_user
              key :in, :body
              key :description, 'Remote user for server to connect to'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :name
              key :in, :body
              key :description, 'Server name'
              key :required, false
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :run_list
              key :in, :body
              key :description, 'Chef run_list to deploy with'
              key :required, false
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            parameter do
              key :name, :public_ip
              key :in, :body
              key :description, 'Server public ip to connect to'
              key :required, false
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response 409 do
              key :description, 'Conflict error, server in invalid state, operation can not be done'
              schema do
                key :'$ref', :DevopsError
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}/tags/add" do
          operation :post do
            key :description, 'Add tags to provider server'
            key :operationId, 'addServerTags'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :tags
              key :in, :body
              key :description, 'Key-value object with tags'
              key :required, false
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}/tags/delete" do
          operation :post do
            key :description, 'Delete tags from provider server'
            key :operationId, 'deleteServerTags'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :tags
              key :in, :body
              key :description, 'Key-value object with tags'
              key :required, false
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/server/{id}/run_list" do
          operation :post do
            key :description, 'Set server run_list'
            key :operationId, 'setServerRunList'
            key :tags, [
              'server'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Server id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :run_list
              key :in, :body
              key :description, 'New server run_list'
              key :required, false
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'Server response'
              schema do
                key :'$ref', :DevopsServerResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

      end
    end
  end
end
