require 'swagger/blocks'

require_relative 'devops_error'
require_relative 'devops_response'

module Devops
  module API3
    module Docs
      class ImageRoutes

        include Swagger::Blocks

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

        swagger_schema :ImageObject do
          allOf do
            schema do
              key :'$ref', :ImageObjectInput
            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 :ImageObjectInput 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 "/images" do
          operation :get do
            key :description, 'Get images list'
            key :operationId, 'getImages'
            key :tags, [
              'image'
            ]
           parameter do
              key :name, :provider
              key :in, :query
              key :description, 'Provider name for filtering images'
              key :required, false
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Images list'
              schema do
                key :type, :array
                items do
                  key :'$ref', :ImageObject
                end
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

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

        swagger_path "/image/(id)" do
          operation :get do
            key :description, 'Get image by id'
            key :operationId, 'getImage'
            key :tags, [
              'image'
            ]
           parameter do
              key :name, :id
              key :in, :path
              key :description, 'Image id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Image object'
              schema do
                key :'$ref', :ImageObject
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/image/(id)" do
          operation :put do
            key :description, 'Update image by id TODO'
            key :operationId, 'updateImage'
            key :tags, [
              'image'
            ]
           parameter do
              key :name, :id
              key :in, :path
              key :description, 'Image id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Devops response'
              schema do
                key :'$ref', :ImageResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

        swagger_path "/image/(id)" do
          operation :delete do
            key :description, 'Delete image by id'
            key :operationId, 'deleteImage'
            key :tags, [
              'image'
            ]
           parameter do
              key :name, :id
              key :in, :path
              key :description, 'Image id'
              key :required, true
              schema do
                key :type, :string
              end
            end
            response 200 do
              key :description, 'Devops response'
              schema do
                key :'$ref', :ImageResponse
              end
            end
            response :default do
              key :description, 'error'
              schema do
                key :'$ref', :DevopsError
              end
            end
          end
        end

      end
    end
  end
end
