# encoding: UTF-8

module Devops
  module API3
    module Routes
      module ProviderRoutes

        # see doc/provider.md
        def self.registered(app)

          app.define_policy :read_provider, "Read providers and its components like security groups, flavors, images ant etc."
          app.define_policy :create_provider_account, "Create provider account", dependencies: [:read_provider]
          app.define_policy :delete_provider_account, "Delete provider account", dependencies: [:read_provider]

          app.get_with_headers "/provider/:provider/account/:account/security_groups" do |provider, account|
            check_policy(:read_provider)
            check_provider(provider)
            json Devops::API3::Handler::Provider.new(request).security_groups(provider, account)
          end

          app.get_with_headers "/provider/:provider/account/:account/flavors" do |provider, account|
            check_policy(:read_provider)
            check_provider(provider)
            json Devops::API3::Handler::Provider.new(request).flavors(provider, account)
          end

          app.get_with_headers "/provider/:provider/account/:account/networks" do |provider, account|
            check_policy(:read_provider)
            check_provider(provider)
            check_provider_account(provider, account)
            json Devops::API3::Handler::Provider.new(request).networks(provider, account)
          end

          app.get_with_headers "/provider/:provider/account/:account/images" do |provider, account|
            check_policy(:read_provider)
            check_provider(provider)
            check_provider_account(provider, account)
            json Devops::API3::Handler::Provider.new(request).images(provider, account)
          end

          app.get_with_headers "/providers" do
            check_policy(:read_provider)
            json Devops::API3::Handler::Provider.new(request).providers
          end

          app.get_with_headers "/providers/available" do
            check_policy(:read_provider)
            json Devops::API3::Handler::Provider.new(request).available_providers
          end

          app.get_with_headers "/provider/:provider/accounts" do |provider|
            check_policy(:read_provider)
            check_provider(provider)
            json Devops::API3::Handler::Provider.new(request).accounts(provider).map(&:to_hash)
          end

          app.post_with_headers "/provider/:provider/account" do |provider|
            check_policy(:create_provider_account)
            check_provider(provider)
            create_response("Created", {:account => Devops::API3::Handler::Provider.new(request).add_account(provider).to_hash}, 201)
          end

          app.delete_with_headers "/provider/:provider/account/:account_name" do |provider, account_name|
            check_policy(:delete_provider_account)
            check_provider(provider)
            check_provider_account(provider, account_name)
            create_response("Deleted", {:account => Devops::API3::Handler::Provider.new(request).delete_account(account_name, provider)})
          end

          app.get_with_headers "/provider/aws/account/:account_name/vpcs" do |account_name|
            check_policy(:read_provider)
            provider = "aws"
            check_provider(provider)
            check_provider_account(provider, account_name)
            json Devops::API3::Handler::Provider.new(request).account_vpcs(provider, account_name)
          end

          puts "Provider routes initialized"
        end
      end
    end
  end
end
