require "db/mongo/models/model_with_provider"
require "db/validators/name"

module Devops
  module Model
    class StackTemplateBase

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

      store_in collection: "stack_templates"

      include ModelWithProviderAccount

      field :_id, as: :id, overwrite: true, type: String
      field :owner, type: String
      field :template_body, type: String
      # should be a hash like {'param_name' => {type: 'String', description: 'Description'}}
      field :available_parameters, type: Hash, default: {}

      validates :owner, :template_body, presence: true
      validates :id, uniqueness: true, on: :create
      validates :id, name: true, presence: true

      def to_hash
        attributes.tap { |hash| hash['id'] = hash.delete('_id') }
      end

      def parse_parameters
        {}
      end

      # attrs should include:
      # - id (String)
      # - provider (String)
      # - template_body (String)
      def self.create(attrs)
        template = new(attrs)
        template.available_parameters = template.parse_parameters
        template
      end
    end
  end
end
