require 'swagger/blocks'

require_relative 'devops_error'
require_relative 'devops_response'

module Devops
  module API3
    module Docs
      class RolesRoutes

        include Swagger::Blocks

        swagger_schema :PolicyObject do
          property :id do
            key :type, :string
          end
          property :description do
            key :type, :string
          end
          property :dependencies do
            key :type, :array
            items do
              key :type, :string
            end
          end
        end

        swagger_schema :RoleObject do
          allOf do
            schema do
              key :'$ref', :RoleObjectInput
            end
            schema do
              property :id do
                key :type, :string
              end
              property :created_at do
                key :type, :integer
                key :format, :int64
              end
            end
          end
        end

        swagger_schema :RoleObjectInput do
          key :required, [:image_id, :name, :provider, :provider_account, :remote_user]
          property :name do
            key :type, :string
            key :pattern, "^[a-z_\s]{1,99}$"
            key :maxLength, 99
            key :minLength, 1
          end
          property :description do
            key :type, :string
            key :maxLength, 500
          end
          property :policies do
            key :type, :array
            items do
              key :type, :string
            end
          end
        end

        swagger_path "/security/policies" do
          operation :get do
            key :description, 'Get available policies list'
            key :operationId, 'getPolicies'
            key :tags, [
              'role'
            ]
            response 200 do
              key :description, 'Policies list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :PolicyObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/security/roles" do
          operation :get do
            key :description, 'Get roles list'
            key :operationId, 'getRoles'
            key :tags, [
              'role'
            ]
            response 200 do
              key :description, 'Roles list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :RoleObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/security/role" do
          operation :post do
            key :description, 'Create role'
            key :operationId, 'createRole'
            key :tags, [
              'role'
            ]
            parameter do
              key :name, :role
              key :in, :body
              key :description, 'New role'
              key :required, true
              schema do
                key :'$ref', :RoleObjectInput
              end
            end
            response 201 do
              key :description, 'Create role response'
              schema do
                key :'$ref', :DevopsResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/security/role/{id}" do
          operation :get do
            key :description, 'Get role by id'
            key :operationId, 'getRole'
            key :tags, [
              'role'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Role id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Role object'
              schema do
                key :'$ref', :RoleObject
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

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

      end
    end
  end
end

