require 'swagger/blocks'

require_relative 'devops_error'
require_relative 'devops_response'

module Devops
  module API3
    module Docs
      class JobTaskRoutes

        include Swagger::Blocks

        swagger_path "/tasks/all" do
          operation :get do
            key :description, 'Get tasks list'
            key :operationId, 'getTasks'
            key :tags, [
              'task'
            ]
            parameter do
              key :name, :date_from
              key :in, :query
              key :description, "Date from in format 'YYYY-MM-DD'"
              key :required, false
              key :type, :string
            end
            parameter do
              key :name, :date_from_l
              key :in, :query
              key :description, "Date from in unixtime"
              key :required, false
              key :type, :integer
            end
            parameter do
              key :name, :date_till
              key :in, :query
              key :description, "Date till in format 'YYYY-MM-DD'"
              key :required, false
              key :type, :string
            end
            parameter do
              key :name, :date_till_l
              key :in, :query
              key :description, "Date till in unixtime"
              key :required, false
              key :type, :integer
            end
            parameter do
              key :name, :sort_field
              key :in, :query
              key :description, "Field to sort by, default value: created_at"
              key :required, false
              key :type, :string
            end
            parameter do
              key :name, :sort_order
              key :in, :query
              key :description, "Sort order, default value: asc"
              key :required, false
              key :type, :string
            end
            parameter do
              key :name, :limit
              key :in, :query
              key :description, "Documents number, default value: 10"
              key :required, false
              key :type, :integer
            end
            parameter do
              key :name, :project
              key :in, :query
              key :description, "Project id"
              key :required, false
              key :type, :string
            end
            parameter do
              key :name, :environment
              key :in, :query
              key :description, "Environment id"
              key :required, false
              key :type, :string
            end
            parameter do
              key :name, :category
              key :in, :query
              key :description, "Category id"
              key :required, false
              key :type, :string
            end
            parameter do
              key :name, :type
              key :in, :query
              key :description, "Job task type"
              key :required, false
              key :type, :integer
            end
            parameter do
              key :name, :server_id
              key :in, :query
              key :description, "Server id"
              key :required, false
              key :type, :string
            end
            parameter do
              key :name, :created_by
              key :in, :query
              key :description, "User how created job task"
              key :required, false
              key :type, :string
            end
            response 200 do
              key :description, 'Tasks response'
              schema do
                key :type, :array
                items do
                  key :'$ref', :TaskResponse
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

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

        swagger_path "/task/{id}/report" do
          operation :get do
            key :description, 'Get task report'
            key :operationId, 'getTaskReport'
            key :tags, [
              'task'
            ]
            key :produces, [
              'text/html'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Task id'
              key :required, true
              key :type, :string
            end
            response 200 do
              key :description, 'Task report'
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/task/{id}/status" do
          operation :get do
            key :description, 'Get task status'
            key :operationId, 'getTaskStatus'
            key :tags, [
              'task'
            ]
            key :produces, [
              'text/html'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Task id'
              key :required, true
              key :type, :string
            end
            response 200 do
              key :description, 'Task status'
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_schema :TaskResponse do
          property :id do
            key :type, :string
          end
          property :server_id do
            key :type, :string
          end
          property :stack do
            key :type, :string
          end
          property :project do
            key :type, :string
          end
          property :environment do
            key :type, :string
          end
          property :category do
            key :type, :string
          end
          property :created_by do
            key :type, :string
          end
          property :status do
            key :type, :string
          end
          property :job_result_code do
            key :type, :integer
          end
          property :type do
            key :type, :integer
          end
          property :created_at do
            key :type, :integer
            key :format, :int64
          end
          property :updated_at do
            key :type, :integer
            key :format, :int64
          end
        end

      end
    end
  end
end
