# Represents a collection of releases with photos
class HeadshotCollection
  attr_reader :collection_uid, :releasables
  
  def self.for_project(project)
    appearance_releases_with_photo = project.appearance_releases.with_person_photo

    new(project.headshot_collection_uid, appearance_releases_with_photo + project.talent_releases)
  end

  def initialize(collection_uid, releasables)
    @collection_uid = collection_uid
    @releasables = releasables
  end

  # Use the custom hash to generate JSON format
  def as_json(*)
    to_hash
  end

  def to_hash
    { 
      collection_uid: collection_uid.to_s,
      bucket_name: aws_bucket_name,
      ids_to_images: map_ids_to_images,
    }.reject { |k, v| v.blank? && k != :ids_to_images }
  end

  private

  def aws_bucket_name
    ENV["AWS_BUCKET"]
  end

  def map_ids_to_images
    releasables.each_with_object({}) do |release, hash|
      hash[release_id(release)] = [release.photo.key] # An array of images is expected, even if there's only one image
    end
  end

  def release_id(release)
    [release.model_name.param_key, release.id].join("_")
  end
end
