# typed: strict
class Domain::Post::SofurryPost < Domain::Post
  include AttrJsonRecordAliases
  include HasAttrJsonEnum

  has_single_creator! Domain::User::SofurryUser
  has_single_file!
  belongs_to_groups! :folders,
                     Domain::PostGroup::SofurryFolder,
                     Domain::PostGroupJoin::SofurryFolderJoin

  validates :sofurry_id, presence: true, uniqueness: true

  attr_json :state, :string
  attr_json :sofurry_id, :integer
  attr_json :title, :string
  attr_json :tags_array, :string, array: true
  attr_json :description, :string
  attr_json :file_name, :string
  attr_json :scanned_at, ActiveModelUtcTimeValue.new

  attr_json :last_scan_log_entry_id, :integer
  belongs_to :last_scan_log_entry, class_name: "::HttpLogEntry", optional: true

  attr_json :last_gallery_log_entry_id, :integer
  belongs_to :last_gallery_log_entry,
             class_name: "::HttpLogEntry",
             optional: true

  scope :for_media_type, ->(media_type) { where(media_type:) }

  attr_json_enum(
    :media_type,
    :integer,
    { stories: 0, art: 1, music: 2, journals: 3, photos: 4 },
    prefix: true,
  )

  attr_json_enum(
    :content_level,
    :integer,
    { clean: 0, adult: 1, extreme: 2 },
    prefix: true,
  )

  enum :state,
       {
         pending: "pending",
         ok: "ok",
         removed: "removed",
         retryable_error: "retryable_error",
         terminal_error: "terminal_error",
       },
       prefix: true
  validates :state, presence: true

  after_initialize { self.state ||= "pending" }

  sig { override.returns(String) }
  def description_html_base_domain
    "sofurry.com"
  end

  sig { override.returns(T.nilable(String)) }
  def description_html_for_view
    description
  end

  sig { override.returns(T.nilable(T.any(String, Integer))) }
  def domain_id_for_view
    self.sofurry_id
  end

  sig { override.returns(T.nilable(Addressable::URI)) }
  def external_url_for_view
    if sfid = self.sofurry_id
      Addressable::URI.parse("https://www.sofurry.com/view/#{sfid}")
    end
  end

  sig { override.returns(T.nilable(Domain::PostFile)) }
  def primary_file_for_view
    self.file
  end

  sig { override.returns(T.nilable(Domain::User)) }
  def primary_creator_for_view
    self.creator
  end

  sig { override.returns(Domain::DomainType) }
  def self.domain_type
    Domain::DomainType::Sofurry
  end

  sig { override.returns([String, Symbol]) }
  def self.param_prefix_and_attribute
    ["sf", :sofurry_id]
  end

  sig { override.returns(String) }
  def self.view_prefix
    "sf"
  end

  sig { override.returns(T.nilable(T::Array[TagForView])) }
  def tags_for_view
    tags_array.map { |value| TagForView.new(category: :general, value:) }
  end

  sig { override.returns(T.nilable(HttpLogEntry)) }
  def scanned_post_log_entry_for_view
    last_scan_log_entry
  end
end
