require 'swagger/blocks'
require_relative "request_handler"

require 'app/api3/docs/devops_error'
require 'app/api3/docs/devops_response'
require 'app/api3/docs/server_model'
require 'app/api3/docs/provider'
require 'app/api3/docs/job_task'
require 'app/api3/docs/chef'
require 'app/api3/docs/filter'
require 'app/api3/docs/image'
require 'app/api3/docs/key'
require 'app/api3/docs/role'
require 'app/api3/docs/user'
require 'app/api3/docs/project'
require 'app/api3/docs/server'

module Devops
  module API3
    module Handler
      class ApiDocs < RequestHandler

        include Swagger::Blocks

        swagger_root do
          key :swagger, '2.0'
          info do
            key :version, '3.0'
            key :title, 'Devops API'
            key :description, 'Devops API v3 specification'
            license do
              key :name, 'MIT'
              key :url, 'https://opensource.org/licenses/MIT'
            end
          end

          tag do
            key :name, 'provider'
            key :description, 'Providers operations'
          end
          tag do
            key :name, 'task'
            key :description, 'Job tasks operations'
          end
          tag do
            key :name, 'chef'
            key :description, 'Chef operations'
          end
          tag do
            key :name, 'filter'
            key :description, 'Filter operations'
          end
          tag do
            key :name, 'image'
            key :description, 'Image operations'
          end
          tag do
            key :name, 'key'
            key :description, 'SSH keys operations'
          end
          tag do
            key :name, 'role'
            key :description, 'User roles operations'
          end
          tag do
            key :name, 'user'
            key :description, 'Users operations'
          end
          tag do
            key :name, 'project'
            key :description, 'Projects operations'
          end
          tag do
            key :name, 'server'
            key :description, 'Servers operations'
          end
#          key :host, 'petstore.swagger.wordnik.com'
          key :basePath, DevopsConfig.config[:url_prefix] + '/v3'
          key :scheme, 'http'
          key :consumes, ['application/json']
          key :produces, ['application/json']
        end

        # A list of all classes that have swagger_* declarations.
        SWAGGERED_CLASSES = [
          Devops::API3::Docs::ProviderRoutes,
          Devops::API3::Docs::JobTaskRoutes,
          Devops::API3::Docs::ChefRoutes,
          Devops::API3::Docs::FilterRoutes,
          Devops::API3::Docs::ImageRoutes,
          Devops::API3::Docs::KeyRoutes,
          Devops::API3::Docs::RolesRoutes,
          Devops::API3::Docs::UserRoutes,
          Devops::API3::Docs::ProjectRoutes,
          Devops::API3::Docs::ServerRoutes,
          Devops::API3::Docs::DevopsError,
          Devops::API3::Docs::DevopsResponse,
          Devops::API3::Docs::ServerModel,
          self
        ].freeze

        def index
          Swagger::Blocks.build_root_json(SWAGGERED_CLASSES)
        end

      end
    end
  end
end
