#######################################################
# This file was auto-generated by elixir_bindings.zig #
#              Do not manually modify.                #
#######################################################

defmodule TigerBeetlex.AccountFilter do
  @moduledoc """
  See [AccountFilter](https://docs.tigerbeetle.com/reference/account-filter#).
  """

  use TypedStruct

  alias TigerBeetlex.AccountFilterFlags

  typedstruct do
    field :account_id, <<_::128>>, default: <<0::size(128)>>
    field :user_data_128, <<_::128>>, default: <<0::size(128)>>
    field :user_data_64, non_neg_integer(), default: 0
    field :user_data_32, non_neg_integer(), default: 0
    field :code, non_neg_integer(), default: 0
    field :timestamp_min, non_neg_integer(), default: 0
    field :timestamp_max, non_neg_integer(), default: 0
    field :limit, non_neg_integer(), default: 0
    field :flags, AccountFilterFlags.t(), default: %AccountFilterFlags{}
  end

  @doc """
  Creates a `TigerBeetlex.AccountFilter` struct from its binary representation.
  """
  @spec from_binary(binary :: <<_::1024>>) :: t()
  def from_binary(<<_::binary-size(128)>> = bin) do
    <<
      account_id::binary-size(16),
      user_data_128::binary-size(16),
      user_data_64::unsigned-little-64,
      user_data_32::unsigned-little-32,
      code::unsigned-little-16,
      _reserved::binary-size(58),
      timestamp_min::unsigned-little-64,
      timestamp_max::unsigned-little-64,
      limit::unsigned-little-32,
      flags::binary-size(4)
    >> = bin

    %__MODULE__{
      account_id: account_id,
      user_data_128: user_data_128,
      user_data_64: user_data_64,
      user_data_32: user_data_32,
      code: code,
      timestamp_min: timestamp_min,
      timestamp_max: timestamp_max,
      limit: limit,
      flags: AccountFilterFlags.from_binary(flags)
    }
  end

  @doc """
  Converts a `TigerBeetlex.AccountFilter` struct to its binary representation.
  """
  @spec to_binary(struct :: t()) :: <<_::1024>>
  def to_binary(struct) do
    %__MODULE__{
      account_id: account_id,
      user_data_128: user_data_128,
      user_data_64: user_data_64,
      user_data_32: user_data_32,
      code: code,
      timestamp_min: timestamp_min,
      timestamp_max: timestamp_max,
      limit: limit,
      flags: flags
    } = struct

    <<
      account_id::binary-size(16),
      user_data_128::binary-size(16),
      user_data_64::unsigned-little-64,
      user_data_32::unsigned-little-32,
      code::unsigned-little-16,
      # reserved
      0::unit(8)-size(58),
      timestamp_min::unsigned-little-64,
      timestamp_max::unsigned-little-64,
      limit::unsigned-little-32,
      AccountFilterFlags.to_binary(flags)::binary-size(4)
    >>
  end
end
