require 'swagger/blocks'

require_relative 'devops_error'
require_relative 'devops_response'

module Devops
  module API3
    module Docs
      class UserRoutes

        include Swagger::Blocks

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

        swagger_schema :UserObject do
          allOf do
            schema do
              key :'$ref', :UserObjectCommon
            end
            schema do
              property :created_at do
                key :type, :integer
                key :format, :int64
              end
            end
          end
        end

        swagger_schema :UserObjectInput do
          allOf do
            schema do
              key :'$ref', :UserObjectCommon
            end
            schema do
              property :password do
                key :type, :string
                key :maxLength, 31
                key :minLength, 5
              end
            end
          end
        end

        swagger_schema :UserEmailBody do
          key :required, [:email]
          property :email do
            key :type, :string
          end
        end

        swagger_schema :UserPasswordBody do
          key :required, [:password]
          property :password do
            key :type, :string
            key :maxLength, 31
            key :minLength, 5
          end
        end

        swagger_schema :UserBodyToUpdate do
          allOf do
            schema do
              key :'$ref', :UserEmailBody
            end
            schema do
              key :'$ref', :UserPasswordBody
            end
            schema do
              property :roles do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
          end
        end

        swagger_schema :UserObjectCommon do
          key :required, [:id, :email, :roles]
          property :id do
            key :type, :string
            key :pattern, "^[\w-]{1,100}$"
            key :maxLength, 100
          end
          property :email do
            key :type, :string
          end
          property :roles do
            key :type, :array
            items do
              key :type, :string
            end
          end
        end

        swagger_path "/users" do
          operation :get do
            key :description, 'Get users list'
            key :operationId, 'getUsers'
            key :tags, [
              'user'
            ]
            response 200 do
              key :description, 'Users list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :UserObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

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

        swagger_path "/user" do
          operation :post do
            key :description, 'Create new user'
            key :operationId, 'createUser'
            key :tags, [
              'user'
            ]
            parameter do
              key :name, :user
              key :in, :body
              key :description, 'New user'
              key :required, true
              schema do
                key :'$ref', :UserObjectInput
              end
            end
            response 201 do
              key :description, 'Create user response'
              schema do
                key :'$ref', :UserResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/user/{id}/roles/add" do
          operation :post do
            key :description, 'Add roles to user'
            key :operationId, 'addRolesToUser'
            key :tags, [
              'user'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'User id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :roles
              key :in, :body
              key :description, 'Roles list'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'User response'
              schema do
                key :'$ref', :UserResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/user/{id}/roles/delete" do
          operation :post do
            key :description, 'Delete roles from user'
            key :operationId, 'deleteRolesFromUser'
            key :tags, [
              'user'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'User id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :roles
              key :in, :body
              key :description, 'Roles list'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'User response'
              schema do
                key :'$ref', :UserResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/user/{id}/email" do
          operation :put do
            key :description, 'Update user email'
            key :operationId, 'userEmail'
            key :tags, [
              'user'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'User id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :email
              key :in, :body
              key :description, 'New email'
              key :required, true
              schema do
                key :'$ref', :UserEmailBody
              end
            end
            response 200 do
              key :description, 'User response'
              schema do
                key :'$ref', :UserResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/user/{id}/password" do
          operation :put do
            key :description, 'Update user password'
            key :operationId, 'userPassword'
            key :tags, [
              'user'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'User id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :password
              key :in, :body
              key :description, 'New password'
              key :required, true
              schema do
                key :'$ref', :UserPasswordBody
              end
            end
            response 200 do
              key :description, 'User response'
              schema do
                key :'$ref', :UserResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

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

        swagger_path "/user/{id}" do
          operation :put do
            key :description, 'Update user by id'
            key :operationId, 'updateUserById'
            key :tags, [
              'user'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'User id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :user_data
              key :in, :body
              key :description, 'User data to update'
              key :required, true
              schema do
                key :'$ref', :UserBodyToUpdate
              end
            end
            response 200 do
              key :description, 'User response'
              schema do
                key :'$ref', :UserResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

      end
    end
  end
end
