# typed: strict
class Domain::UserAvatar < ReduxApplicationRecord
  include AttrJsonRecordAliases
  self.table_name = "domain_user_avatars"
  belongs_to :user, class_name: "::Domain::User", inverse_of: :avatar

  attr_json :url_str, :string
  attr_json :state, :string
  attr_json :downloaded_at, ActiveModelUtcTimeValue.new
  attr_json :error_message, :string
  attr_json :last_log_entry_id, :integer
  attr_json :log_entry_id, :integer

  belongs_to :last_log_entry, class_name: "::HttpLogEntry", optional: true
  belongs_to :log_entry, class_name: "::HttpLogEntry", optional: true

  enum :state,
       {
         pending: "pending",
         ok: "ok",
         file_404: "file_404",
         http_error: "http_error",
       },
       prefix: "state"

  validates :state,
            presence: true,
            inclusion: {
              in: %w[pending ok file_404 http_error],
            }

  after_initialize { self.state ||= "pending" if new_record? }
end
