require "db/mongo/models/model_with_provider_account"
require "providers/static"

module Devops
  module Model
    class Server

      include ::Mongoid::Document
      include ::Mongoid::Timestamps::CreatedInt
      include ::ActiveModel::Validations

      store_in collection: "servers"

      include ModelWithProviderAccount

      module OperationType
        # we store strings in mongo, so it's better not to use symbols
        CREATION = 'creation'
        BOOTSTRAP = 'bootstrap'
        DEPLOY = 'deploy'
        DEPLOY_INFO = 'deploy_info'
        RESERVE = 'reserve'
        UNRESERVE = 'unreserve'
        PAUSE = 'pause'
        UNPAUSE = 'unpause'

        def self.supported_type?(value)
          return false unless value.is_a?(String)
          [CREATION, DEPLOY, RESERVE, UNRESERVE, PAUSE, UNPAUSE, BOOTSTRAP, DEPLOY_INFO].include?(value)
        end
      end

      field :_id, as: :'id', overwrite: true, type: String
      field :name, type: String
      field :cm_name, type: String
      field :remote_user, type: String
      field :project, type: String
      field :environment, type: String
      field :category, type: String
      field :private_ip, type: String
      field :public_ip, type: String
      field :created_by, type: String
      field :reserved_by, type: String
      field :stack, type: String
      field :run_list, type: Array, default: []
      field :ssh_key, type: String
      field :last_operation, type: Hash

      validates :remote_user, :project, :environment, :category, :private_ip, :ssh_key, :created_by,
        :name, presence: true
      validates :run_list, runListArray: {allow_nil: true}

=begin
      def self.fields
        ["name", "project", "environment", "provider", "remote_user", "private_ip", "public_ip", "created_at", "created_by", "key", "reserved_by", "run_list", "stack",
          "last_operation"]
      end
=end

      def initialize hash
#        raise InvalidRecord.new("Parameter 'id' is not a string") unless hash["id"].is_a?(String)
        super(hash)
      end

      def to_hash
        hash = self.attributes.clone
        hash["id"] = hash.delete("_id")
        hash
      end

      def info
        str = "Instance Name: #{self.name}\n"
        str << "Instance ID: #{self.id}\n"
        str << "Private IP: #{self.private_ip}\n"
        str << "Public IP: #{self.public_ip}\n" unless self.public_ip.nil?
        str << "Remote user: #{self.remote_user}\n"
        str << "Project: #{self.project}\n"
        str << "Environment: #{self.environment}\n"
        str << "Category: #{self.category}\n"
        str << "Created by: #{self.created_by}"
        str
      end

      def static?
        self.provider == ::Provider::Static::PROVIDER
      end

      def set_last_operation(operation_type, user, job_task)
        raise ArgumentError unless OperationType.supported_type?(operation_type)
        self.last_operation = {
          'type' => operation_type,
          'date' => Time.now.to_i,
          'user' => user,
          'job_task' => job_task
        }
        self.save
      end

      def self.stack_servers(stack_id)
        where('stack' => stack_id)
      end

      def self.delete_stack_servers(stack_id)
        stack_servers(stack_id).delete
      end

    end
  end
end
