defmodule Domain.Gateways.Gateway do
  use Domain, :schema

  @type t :: %__MODULE__{
          id: Ecto.UUID.t(),
          external_id: String.t(),
          name: String.t(),
          public_key: String.t(),
          psk_base: binary(),
          ipv4: Domain.Types.IP.t(),
          ipv6: Domain.Types.IP.t(),
          last_seen_user_agent: String.t(),
          last_seen_remote_ip: Domain.Types.IP.t(),
          last_seen_remote_ip_location_region: String.t(),
          last_seen_remote_ip_location_city: String.t(),
          last_seen_remote_ip_location_lat: float(),
          last_seen_remote_ip_location_lon: float(),
          last_seen_version: String.t(),
          last_seen_at: DateTime.t(),
          online?: boolean(),
          account_id: Ecto.UUID.t(),
          group_id: Ecto.UUID.t(),
          deleted_at: DateTime.t(),
          inserted_at: DateTime.t(),
          updated_at: DateTime.t()
        }

  schema "gateways" do
    field :external_id, :string

    field :name, :string

    field :public_key, :string
    field :psk_base, :binary, read_after_writes: true

    field :ipv4, Domain.Types.IP
    field :ipv6, Domain.Types.IP

    field :last_seen_user_agent, :string
    field :last_seen_remote_ip, Domain.Types.IP
    field :last_seen_remote_ip_location_region, :string
    field :last_seen_remote_ip_location_city, :string
    field :last_seen_remote_ip_location_lat, :float
    field :last_seen_remote_ip_location_lon, :float
    field :last_seen_version, :string
    field :last_seen_at, :utc_datetime_usec

    field :online?, :boolean, virtual: true

    belongs_to :account, Domain.Accounts.Account
    belongs_to :group, Domain.Gateways.Group

    # TODO: HARD-DELETE - Remove field after soft deletion is removed
    field :deleted_at, :utc_datetime_usec
    timestamps()
  end
end
