require 'swagger/blocks'

require_relative 'devops_error'
require_relative 'devops_response'
require_relative 'server_model'

module Devops
  module API3
    module Docs
      class ProjectRoutes

        include Swagger::Blocks

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

        swagger_schema :CreateCategoryResponse do
          allOf do
            schema do
              key :'$ref', :DevopsResponse
            end
            schema do
              property :category do
                key :'$ref', :CategoryObject
              end
            end
          end
        end

        swagger_schema :CreateEnvironmentResponse do
          allOf do
            schema do
              key :'$ref', :DevopsResponse
            end
            schema do
              property :environment do
                key :'$ref', :CreateEnvironmentObject
              end
            end
          end
        end

        swagger_schema :ProjectCreate do
          property :id do
            key :type, :string
            key :maxLength, 100
          end
          property :description do
            key :type, :string
            key :maxLength, 500
          end
          property :run_list do
            key :type, :array
            items do
              key :type, :string
            end
          end
        end

        swagger_schema :CreateEnvironmentObject do
          property :id do
            key :type, :string
            key :maxLength, 100
          end
          property :expires do
            key :type, :string
          end
          property :run_list do
            key :type, :array
            items do
              key :type, :string
            end
          end
          property :users do
            key :type, :array
            items do
              key :type, :string
            end
          end
        end

        swagger_schema :ServerLastOperationObject do
          property :type do
            key :type, :string
          end
          property :date do
            key :type, :int64
          end
          property :user do
            key :type, :string
          end
        end

        swagger_schema :EnvUsersResponse do
          allOf do
            schema do
              key :'$ref', :ProjectResponse
            end
            schema do
              property :users do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
          end
        end

        swagger_schema :ProjectUsersResponse do
          allOf do
            schema do
              key :'$ref', :ProjectResponse
            end
            schema do
              property :project_users do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
          end
        end

        swagger_schema :ProjectRunListResponse do
          allOf do
            schema do
              key :'$ref', :ProjectResponse
            end
            schema do
              property :run_list do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
          end
        end

        swagger_schema :CategoryProviderObject do
          property :name do
            key :type, :string
          end
          property :account do
            key :type, :string
          end
          property :flavor do
            key :type, :string
          end
          property :image do
            key :type, :string
          end
          property :subnet do
            key :type, :string
          end
          property :vpc_id do
            key :type, :string
          end
          property :security_group do
            key :type, :array
            items do
              key :type, :string
            end
          end
          property :stack_template do
            key :type, :string
          end
        end

        swagger_schema :CategoryCmToolObject do
          property :name do
            key :type, :string
          end
          property :bootstrap_template do
            key :type, :string
          end
        end

        swagger_schema :CategoryObject do
          property :id do
            key :type, :string
            key :maxLength, 100
          end
          property :provider do
            key :'$ref', :CategoryProviderObject
          end
          property :cm_tool do
            key :'$ref', :CategoryCmToolObject
          end
        end

        swagger_schema :EnvironmentObject do
          property :id do
            key :type, :string
            key :maxLength, 100
          end
          property :expires do
            key :type, :string
          end
          property :run_list do
            key :type, :array
            items do
              key :type, :string
            end
          end
          property :users do
            key :type, :array
            items do
              key :type, :string
            end
          end
          property :categories do
            key :type, :array
            items do
              key :'$ref', :CategoryObject
            end
          end
        end

        swagger_schema :ProjectShowObject do
          allOf do
            schema do
              key :'$ref', :ProjectListObject
            end
            schema do
              property :archived do
                key :type, :boolean
              end
              property :environments do
                key :type, :array
                items do
                  key :'$ref', :EnvironmentObject
                end
              end
            end
          end
        end

        swagger_schema :ProjectUpdateObject do
          property :description do
            key :type, :string
            key :maxLength, 500
          end
          property :run_list do
            key :type, :array
            items do
              key :type, :string
            end
          end
          property :project_users do
            key :type, :array
            items do
              key :type, :string
            end
          end
        end

        swagger_schema :ProjectListObject do
          allOf do
            schema do
              key :'$ref', :ProjectUpdateObject
            end
            schema do
              property :id do
                key :type, :string
                key :maxLength, 100
              end
              property :owner do
                key :type, :string
              end
              property :created_at do
                key :type, :integer
                key :format, :int64
              end
            end
          end
        end

        swagger_schema :ProjectObjectInput do
          key :required, [:image_id, :name, :provider, :provider_account, :remote_user]
          property :image_id do
            key :type, :string
            key :pattern, "^[a-z0-9_-]{0,99}$"
            key :maxLength, 100
          end
          property :name do
            key :type, :string
            key :maxLength, 255
          end
          property :remote_user do
            key :type, :string
            key :pattern, "^[a-z_][a-z0-9_-]{0,30}$"
            key :maxLength, 31
          end
          property :provider do
            key :type, :string
          end
          property :provider_account do
            key :type, :string
            key :pattern, "^[a-z_][a-z0-9_-]{0,99}$"
            key :maxLength, 100
          end
        end

        swagger_path "/projects" do
          operation :get do
            key :description, 'Get projects list'
            key :operationId, 'getProjects'
            key :tags, [
              'project'
            ]
            response 200 do
              key :description, 'Projects list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :ProjectListObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

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

        swagger_path "/project/{id}" do
          operation :put do
            key :description, 'Update project by id'
            key :operationId, 'updateProjectById'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :project
              key :in, :body
              key :description, 'Project parameters to rewrite'
              key :required, true
              schema do
                key :'$ref', :ProjectUpdateObject
              end
            end
            response 200 do
              key :description, 'Project response'
              schema do
                key :'$ref', :ProjectResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

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

        swagger_path "/project" do
          operation :post do
            key :description, 'Create project'
            key :operationId, 'createProject'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :body
              key :description, 'New project body'
              key :required, true
              schema do
                key :'$ref', :ProjectCreate
              end
            end
            response 201 do
              key :description, 'Create project response'
              schema do
                key :'$ref', :ProjectResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{id}/users/add" do
          operation :post do
            key :description, 'Add users to project'
            key :operationId, 'addUsersToProject'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :users
              key :in, :body
              key :description, 'Add users to project list'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'Project users response'
              schema do
                key :'$ref', :ProjectUsersResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{id}/users/delete" do
          operation :post do
            key :description, 'Delete users from project'
            key :operationId, 'deleteUsersFromProject'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :users
              key :in, :body
              key :description, 'Delete users from project list'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'Project response'
              schema do
                key :'$ref', :ProjectUsersResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{id}/run_list" do
          operation :put do
            key :description, 'Set new run_list elements to project'
            key :operationId, 'setRunListToProject'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :run_list
              key :in, :body
              key :description, 'run_list elements to set to project list'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'Project response'
              schema do
                key :'$ref', :ProjectRunListResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{id}/run_list/add" do
          operation :post do
            key :description, 'Add new run_list elements to project'
            key :operationId, 'addRunListToProject'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :run_list
              key :in, :body
              key :description, 'run_list elements to add to project list'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'Project response'
              schema do
                key :'$ref', :ProjectRunListResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{id}/run_list/delete" do
          operation :post do
            key :description, 'Delete run_list elements from project'
            key :operationId, 'deleteRunListFromProject'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :id
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :users
              key :in, :body
              key :description, 'run_list elements to delete from project list'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'Project response'
              schema do
                key :'$ref', :ProjectRunListResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/archive" do
          operation :post do
            key :description, 'Archive project'
            key :operationId, 'archiveProject'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Project response'
              schema do
                key :'$ref', :ProjectResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/unarchive" do
          operation :post do
            key :description, 'Unarchive project'
            key :operationId, 'unarchiveProject'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Project response'
              schema do
                key :'$ref', :ProjectResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environment/{env}/users/add" do
          operation :post do
            key :description, 'Add users to environment'
            key :operationId, 'addUsersToEnvironment'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :users
              key :in, :body
              key :description, 'Users list to add to environment list'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'Environment users response'
              schema do
                key :'$ref', :EnvUsersResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environment/{env}/users/delete" do
          operation :post do
            key :description, 'Delete users from environment'
            key :operationId, 'deleteUsersFromEnvironment'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :users
              key :in, :body
              key :description, 'Users list to delete from environment list'
              key :required, true
              schema do
                key :type, :array
                items do
                  key :type, :string
                end
              end
            end
            response 200 do
              key :description, 'Environment users response'
              schema do
                key :'$ref', :EnvUsersResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/servers" do
          operation :get do
            key :description, 'Show project servers'
            key :operationId, 'showProjectServers'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Project servers response'
              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 "/project/{project}/environment/{env}/servers" do
          operation :get do
            key :description, 'Show environment servers'
            key :operationId, 'showEnvironmentServers'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Environment servers response'
              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 "/project/{project}/environment/{env}/stacks" do
          operation :get do
            key :description, 'Show environment stacks'
            key :operationId, 'showEnvironmentStacks'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Environment stacks response'
              schema do
                key :type, :array
                items do
                  key :'$ref', :StackObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environments" do
          operation :get do
            key :description, 'Get project environments'
            key :operationId, 'getProjectEnvironments'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Environments list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :EnvironmentObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environment" do
          operation :post do
            key :description, 'Create project environment'
            key :operationId, 'createProjectEnvironment'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              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 object'
              key :required, true
              schema do
                key :'$ref', :CreateEnvironmentObject
              end
            end
            response 200 do
              key :description, 'Create environment response'
              schema do
                key :'$ref', :CreateEnvironmentResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environment/{env}" do
          operation :get do
            key :description, 'Show project environment'
            key :operationId, 'showProjectEnvironment'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Environment object'
              schema do
                key :'$ref', :EnvironmentObject
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environment/{env}" do
          operation :delete do
            key :description, 'Delete project environment'
            key :operationId, 'deleteProjectEnvironment'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Delete environment response'
              schema do
                key :'$ref', :CreateEnvironmentResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environment/{env}/categories" do
          operation :get do
            key :description, 'Get environment categories'
            key :operationId, 'getEnvironmentCategories'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Categories list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :CategoryObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environment/{env}/category/{category}" do
          operation :get do
            key :description, 'Get category by id'
            key :operationId, 'getCategoryById'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :category
              key :in, :path
              key :description, 'Category id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Category'
              schema do
                key :'$ref', :CategoryObject
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environment/{env}/category" do
          operation :post do
            key :description, 'Create new category'
            key :operationId, 'createCategory'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              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 object'
              key :required, true
              schema do
                key :'$ref', :CategoryObject
              end
            end
            response 200 do
              key :description, 'Create category response'
              schema do
                key :'$ref', :CreateCategoryResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/project/{project}/environment/{env}/category/{category}" do
          operation :delete do
            key :description, 'Delete category by id'
            key :operationId, 'deleteCategoryById'
            key :tags, [
              'project'
            ]
            parameter do
              key :name, :project
              key :in, :path
              key :description, 'Project id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :env
              key :in, :path
              key :description, 'Environment id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            parameter do
              key :name, :category
              key :in, :path
              key :description, 'Category id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Delete category response'
              schema do
                key :'$ref', :CreateCategoryResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

      end
    end
  end
end
