class Api::V1::Inputs::CreatePositionInput < Common::Resource
  field :external_user_id, type: :string
  field :external_user_email, type: :string
  field :withdrawal_address, type: :string
  field :validators_count, type: :integer
  field :eth2_network_name, type: :string, default: 'mainnet'

  validates :external_user_id, presence: true
  validates :external_user_email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
  validates :validators_count, presence: true, numericality: { only_integer: true, greater_than: 0 }
  validates :eth2_network_name, inclusion: { in: ->(_) { Prime::Chains::Beacon.pluck(:slug) } }
  validate :validate_address_type

  class Serializer
    include JSONAPI::Serializer

    attributes(*Api::V1::Inputs::CreatePositionInput.mapping.keys)
  end

  private

  def validate_address_type
    errors.add(:withdrawal_address, :invalid) unless AdequateCryptoAddress.valid?(withdrawal_address, :ETH)
  end
end
