# frozen_string_literal: true


module MaterialRepr
  BASE = { only: [:id, :url, :version_no, :source_kind, :source_uri,
                  :source_path, :source_file_id, :normalized_source_key,
                  :created_at, :updated_at],
           methods: [:content_type],
           include: { tag: TagRepr::BASE,
                      created_by_user: UserRepr::BASE,
                      updated_by_user: UserRepr::BASE } }.freeze

  module_function

  def base material, host:
    material.as_json(BASE).merge(
        file: if material.file.attached?
                Rails.application.routes.url_helpers.rails_storage_proxy_url(
                    material.file, host:)
              end,
        thumbnail: thumbnail_url(material, host:),
        thumbnail_fallback_text: thumbnail_fallback_text(material),
        thumbnail_fallback_kind: thumbnail_fallback_kind(material),
        media_kind: media_kind(material),
        file_byte_size: material.file_byte_size,
        export_paths: export_paths(material),
        export_items: export_items(material))
  end

  def many materials, host:
    materials.map { |m| base(m, host:) }
  end

  def list material, host:
    { id: material.id,
      version_no: material.version_no,
      url: material.url,
      source_kind: material.source_kind,
      source_uri: material.source_uri,
      source_path: material.source_path,
      source_file_id: material.source_file_id,
      normalized_source_key: material.normalized_source_key,
      tag: compact_tag(material.tag),
      thumbnail: thumbnail_url(material, host:),
      thumbnail_fallback_text: thumbnail_fallback_text(material),
      thumbnail_fallback_kind: thumbnail_fallback_kind(material),
      media_kind: media_kind(material),
      content_type: material.content_type,
      file_byte_size: material.file_byte_size,
      created_at: material.created_at,
      updated_at: material.updated_at,
      export_paths: export_paths(material),
      export_items: export_items(material) }
  end

  def list_many materials, host:
    materials.map { |m| list(m, host:) }
  end

  def export_paths material
    material.material_export_items.each_with_object({ }) do |item, hash|
      hash[item.profile] = item.enabled ? item.export_path : ''
    end
  end

  def export_items material
    material.material_export_items.map do |item|
      { id: item.id,
        profile: item.profile,
        export_path: item.export_path,
        enabled: item.enabled }
    end
  end

  def thumbnail_url material, host:
    return nil unless material.thumbnail.attached?

    Rails.application.routes.url_helpers.rails_storage_proxy_url(
        material.thumbnail, host:)
  end

  def thumbnail_fallback_text material
    material.tag&.name || material.created_at&.strftime('%Y-%m-%d')
  end

  def thumbnail_fallback_kind material
    material.tag.present? ? 'tag_name' : 'created_at'
  end

  def media_kind material
    return 'url_only' unless material.file.attached?

    content_type = material.file.blob.content_type.to_s
    return 'image' if content_type.start_with?('image/')
    return 'video' if content_type.start_with?('video/')
    return 'audio' if content_type.start_with?('audio/')

    'file_other'
  end

  def compact_tag tag
    return nil unless tag

    { id: tag.id,
      name: tag.name,
      category: tag.category,
      deprecated_at: tag.deprecated_at }
  end
end
