require 'swagger/blocks'

require_relative 'devops_error'
require_relative 'devops_response'

module Devops
  module API3
    module Docs
      class KeyRoutes

        include Swagger::Blocks

        swagger_schema :KeyObject do
          property :id do
            key :type, :string
          end
          property :filename do
            key :type, :string
          end
          property :created_at do
            key :type, :integer
            key :format, :int64
          end
        end

        swagger_schema :KeyObjectInput do
          key :required, [:file_name, :key_name, :content]
          property :file_name do
            key :type, :string
            key :pattern, "^[a-z0-9_-]{0,99}$"
            key :maxLength, 100
          end
          property :key_name do
            key :type, :string
            key :pattern, "^[a-z0-9_-]{0,99}$"
            key :maxLength, 100
          end
          property :content do
            key :type, :string
          end
        end

        swagger_path "/keys" do
          operation :get do
            key :description, 'Get keys list'
            key :operationId, 'getKeys'
            key :tags, [
              'key'
            ]
            response 200 do
              key :description, 'Keys list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :KeyObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/key" do
          operation :post do
            key :description, 'Create new key'
            key :operationId, 'createKey'
            key :tags, [
              'key'
            ]
            parameter do
              key :name, :key
              key :in, :body
              key :description, 'New key object'
              key :required, true
              schema do
                key :'$ref', :KeyObjectInput
              end
            end
            response 201 do
              key :description, 'Devops response'
              schema do
                key :type, :array
                items do
                  key :'$ref', :DevopsResponse
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/key/{id}" do
          operation :delete do
            key :description, 'Delete key by id'
            key :operationId, 'deleteKey'
            key :tags, [
              'key'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Key id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Devops response'
              schema do
                key :type, :array
                items do
                  key :'$ref', :DevopsResponse
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

      end
    end
  end
end
